Original: Django Learning notes website Tutorial correcting code
Django Learning Notes
4, template beginners, according to the example Django book the following exception
Raise Importerror ("Settings cannot be imported, because environment variable%s is undefined."% environment_variable)
Importerror:settings cannot be imported, because environment variable django_settings_module is undefined.
Google has to look for:
From django.template import Template,context
Add the following two sentences
From django.conf Import settings
Settings.configure ()
t = Template ("My name is {{name}}.")
c = Context ({"Name": "Stipho"})
T.render (c)
#
The sixth chapter, admin management study
Follow the example to make the following happen:
Importerror at/admin/
Exception value:no module named URLs
The regular in the URL is: urlpatterns = Patterns (", (R ' ^admin/', include (' Django.contrib.admin.urls '))
This is the old version of the settings. This should be set in the new version (1.2.1):
Set in settings.py:
Installed_apps = (
' Django.contrib.auth ',
' Django.contrib.contenttypes ',
' Django.contrib.sessions ',
' Django.contrib.admin ', # # #注意, 1.2.1 Be sure to add
' Mydjango.books ',
)
Set in urls.py:
Add to
From Django.contrib Import admin
Uncomment:
Admin.autodiscover ()
Set in URL pattern:
Urlpatterns = Patterns (",
(R ' ^admin/(. *) ', admin.site.root),
)
If you need to open Admindoc, the following additional settings are required:
Added in Installed_apps:
' Django.contrib.admindocs ',
Notes in urls.py:
Urlpatterns = Patterns (",
(R ' ^admin/doc/', include (' Django.contrib.admindocs.urls ')),
)
In addition, Python's Docutils class library is required, but not found under Windows.
: http://docutils.sourceforge.net/
The background of Chinese:
Change Language_code from en-us to ZH-CN in setting.py
Q: You can log in and find nothing to manage (i.e. "Master Admin index" as described in the manual)
A: Build admin.py in the app you need to manage (here is books), add the modules you need to manage
From mysite.books.models Import *
From Django.contrib Import admin
Admin.site.register (Publisher)
Admin.site.register (Author)
Admin.site.register (book)
Restart Server
Q: What if I accidentally delete an admin user?
A:
When you run SYNCDB for the first time, you are likely to be asked whether to create a superuser, and if you did not create superuser at that time,
You need to run djang/contrib/auth/bin/create_superuser.py to create an admin user, otherwise you won't be able to log in to the Admin interface! method, as follows:
(1) in the new version (1.2.1) under Windows: E:\workspace\mydjango>python manage.py Createsuperuser then enter the user name, mailbox, password, etc.
(2) using python/path/to/django/contrib/auth/create_superuser.py in the old version
Reference: http://docs.djangoproject.com/en/1.2/topics/auth/#creating-superusers
#
Seventh chapter, form processing
Q: When running an example
Could not import mydjango.books.views. Error was:cannot Import Name newforms
A: The reason is 1.2.1 version of the only forms this module, put
From Django import newforms as forms to from Django import forms!
Q: When the form is submitted, it appears:
Forbidden (403)
CSRF verification failed. Request aborted.
A: The reason is that Django protects the submitted data against cross-site attacks, so a form token verification is required
Reference here: http://docs.djangoproject.com/en/dev/ref/contrib/csrf/
Workaround: Found in settings.py
Middleware_classes = (
' Django.middleware.common.CommonMiddleware ',
' Django.contrib.sessions.middleware.SessionMiddleware ',
' Django.middleware.csrf.CsrfViewMiddleware ',
' Django.middleware.csrf.CsrfResponseMiddleware ', # # #添加这句, used for form token validation
' Django.contrib.auth.middleware.AuthenticationMiddleware ',
' Django.contrib.messages.middleware.MessageMiddleware ',
)
Also add validation token fields to the template:
{% Csrf_token%}
Q: The following exception occurred while submitting the form and validating the form:
' Contactform ' object has no attribute ' Clean_data '
Cause: The new forms module has changed clean_data to Cleaned_data
Solution: Put Clean_data=>cleaned_data
Q: Using the From django.forms import Form_for_model will appear:
Error was:cannot Import Name Form_for_model
Cause: A new name was used for the model definition of the form in the new version (1.2.1)
Workaround:
Reference http://docs.djangoproject.com/en/1.2/topics/forms/modelforms/
From django.forms import Modelform # # #重新定义
Class Publisherform (Modelform): # # #定义元数据
Class Meta:
Model = Publisher
#
Eighth Chapter
View Focus: URLs with named groups
(R ' articles/(? P\D{4})/', ' year_archive '), # #把year当成形参
(R ' articles/(? P\D{4})/(? P\D{2})/', ' month_archive '), # Year and month are formal parameters
Definition of the function:
In Mydjango.views
Year Archive
def year_archive (request,year):
html = ' Current year was: ', year
return HttpResponse (HTML)
Month Archive
def month_archive (request,month,year):
html = ' Current year and month are: ', year, '-', month
return HttpResponse (HTML)
It is important to note that if you use named groups in Urlconf, named groups and non-named groups cannot exist in the same urlconf mode at the same time.
If you do this, Django will not throw any errors, but you may find that your URLs are not matched correctly as you might expect.
如果有任何命名的组,Django会忽略非命名组而直接使用命名组。
§ Otherwise, Django passes all non-named groups as positional parameters.
In both cases, Django also passes some extra parameters in the same way as the keyword argument
Django Learning Notes website Tutorial correcting code