Django url, static file, POST request configuration, djangourl
Pycharm is usually used, so this article mainly explains how to use the default django project created by pycharm. The project directory is as follows:
1. URL Configuration
After creating a project, run the project to view the default page of django. How can I access my own page?
Because django is a MVC-like development model. This involves URL configuration. Create your ownAPP
(Also known as a module), including the following file content:
Then you needsetting.py
FileINSTALLED_APPS
To register the newly createdApp
:
The default url configuration for the newly created project is as follows:
from django.conf.urls import patterns, include, urlfrom django.contrib import adminadmin.autodiscover()urlpatterns = patterns('', # Examples: # url(r'^$', 'django_static_file_config.views.home', name='home'), # url(r'^blog/', include('blog.urls')), url(r'^admin/$', include(admin.site.urls)), )
So how can we change the newly createdApp
Ofurls.py
And the default Projecturls.py
What about Association? The following configuration is required:
From django. conf. urls import patterns, include, urlfrom django. contrib import adminadmin. autodiscover () urlpatterns = patterns ('', # Examples: # url (r '^ $', 'django _ static_file_config.views.home ', name = 'home '), # url (R' ^ blog/', include ('blog. urls '), url (R' ^ admin/$', include (admin. site. urls), # Use the include () function to include the url (R' ^ load/', include ('index. urls ')),)
In the newly created APPurls.py
The configuration is as follows:
#!usr/bin/env python# coding: utf-8from django.conf.urls import patternsfrom django.conf.urls import urlurlpatterns = patterns('', url(r'^load_index/$', 'index.view.load_index', name='load_index'), )
The access url is:
Http: // 127.0.0.1: 8000/load/load_index/
The significance of the preceding two configuration files is:
When django receives the request, it will follow the first layer in the url level (in the example/load/
) To scan the default configuration file, that is, the following statement:
url(r'^load/', include('index.urls')),
If you can scan, follow the second layer.load_index
Scan the include import url, that is, the newly created APP in the projecturls.py
Is there a match in the url.
2. static file configuration
When I was learning django, I spent a lot of time on static file configuration and did not solve the problem. I also searched a lot of solutions on the Internet, but still did not apply to my own projects. Because different versions of django will change, we strongly recommend that you read the official document based on your current version:
First, read the django official documentation (my version is django1.6 ):
Https://docs.djangoproject.com/en/1.6/howto/static-files/
A default configuration file is generated in the root directory of the project when a django project is created.setting.py
:
The default static file configuration is as follows:
# Static files (CSS, JavaScript, Images) # Here we have generated the address of the official document # https://docs.djangoproject.com/en/1.6/howto/static-files/STATIC_URL = '/static/' TEMPLATE _ DIRS = (OS. path. join (BASE_DIR, 'templates '),)
After reading the official documentation (Strongly recommended), We only needsetting.py
Add the following code to specify the directory of the static file:
# -----config static fileSTATICFILES_DIRS = ( os.path.join(BASE_DIR, "static"),)
When I browse the Web page, I can find the static file correctly. For example:
3. POST request configuration
Readingpost
Before the request, take a look.get
Request method. The front-end jQuery code is as follows:
$(function () { $('.test-post').on('click', function () { var url = '/load/getmethod/' $.ajax({ type: 'GET', data:{ test:'test' }, url: url, success: testSuccess, dataType: 'html', async: false }); });})var testSuccess =function (){};
Backend python processing method:
def getmethod(request): test = request.GET.get('test','') return HttpResponse(test)
Can receiveget
Parameters:
Check the result, for example:
UsePOST
What will happen?
You only need to modify the parameters in jQuery code. First, modify the url:
var url = '/load/postmethod/'
Then modify:
type: 'POST',
When you try to pass parameters on the backend again, the result is as follows:
Occurred403
The request is rejected. This is because the default configuration of djangoMIDDLEWARE_CLASSES
MiddlewareCSRF
(Cross-Site Request Forgery), Wikipedia address (Click here ):
'django.middleware.csrf.CsrfViewMiddleware',
Pairpost
The request is verified.
Solution 1:
First, we try to comment out this line to verify whether it can be sent normally.POST
Request.
The result is:OK
Such:
Solution 2:
Import
from django.views.decorators.csrf import csrf_exempt
Module, andpost
Add the following prefix before the Parameter Function,
@csrf_exemptdef postmethod(request): test = request.POST.get('test','') return HttpResponse(test)
Verified and acceptedpost
Request (one disadvantage is that allPOST
The request processing function must be marked as follows ):
CSRF recommended reading:
Http://www.cnblogs.com/lins05/archive/2012/12/02/2797996.html