Django Handbook Learn Notes

Source: Internet
Author: User

Django Learning Note 4, template beginner, according to the book Example Django Books, 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_setti  Ngs_module is undefined. Google get: from django.template import template,context### plus the following two sentences from django.conf import settings Settings.configure () t = Template ("My name is {{name}}.") c = Context ({"Name": "Stipho"}) T.render (c) #################################### #第六章, admin Management Learning follows the example: the Importerror at/admin/exception value:no module named Urlsurl 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 ', ' Djang O.contrib.sessions ', ' Django.contrib.admin ', # # #注意, 1.2.1 Be sure to add ' mydjango.books ',) set in urls.py: Add from Django.contrib I Mport Admin Uncomment: Admin.autodiscover () set in URL pattern: urlpatterns = PATterns ("," (R ' ^admin/(. *) ', admin.site.root),) # # #如需要打开admindoc, you need the following additional settings: Installed_apps add: ' Django.contrib.admindocs ', urls.py in notes: Urlpatterns = Patterns ("," (R ' ^admin/doc/', include (' Django.contrib.admindocs.urls '), in addition, Python's Docutils class library is also required, but not found under Windows. : http://docutils.sourceforge.net/Chinese background: in setting.py language_code from en-us to ZH-CN Q: You can log in but found no content to manage (that is, the "Master Management Index" in the manual) A: In the app that needs to be managed (here is books), build the admin.py, add the modules that need to be managed from mysite.books.models import *from django.contrib Import admin Admin.site.register (Publisher) admin.site.register (Author) admin.site.register (book) and restart Serverq: What if you 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, if you did not create superuser, you need to run djang/contrib/auth/bin/create_superuser.py To create an admin user, otherwise you will not be able to login 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, the mailbox, passwords, etc. (2) used in older versions python/path/to/django/contrib/auth/create_superuser.py reference: HTTP://DOCS.DJANGOPROJECT.COM/EN/1.2/ topics/auth/#creating-superusers################################# #第七章, form processing Q: Could not import mydjango.books.views when running an example.  Error was:cannot Import name NEWFORMSA: The reason is 1.2. The 1 version of Forms is the only module that changes from Django import newforms as forms to the From Django Import Forms Can! Q: When the form is submitted, it appears: Forbidden (403) CSRF verification failed. Request aborted. A: The reason is that Django will protect the submitted data against cross-site attacks, so a form token verification reference is required here: http://docs.djangoproject.com/en/dev/ref/contrib/csrf/ Workaround: Find middleware_classes in settings.py = (' Django.middleware.common.CommonMiddleware ', ' DJANGO.CONTRIB.SESSIONS.MI Ddleware. Sessionmiddleware ', ' django.middleware.csrf.CsrfViewMiddleware ', ' Django.middleware.csrf.CsrfResponseMiddleware ' , # # #添加这句, used for form token validation ' django.contrib.auth.middleware.AuthenticationMiddleware ', ' Django.contrib.messages.middleware.MessageMiddleware ',) additionally add the validated token field in the template: <form action= "" method= "POST" > {% Csrf_token%}---------------------------------------Q: Submits the form, and the following exception occurs when validating the form: ' Contactform ' object has no attribute ' Clean_data ' Cause: The new forms module has changed clean_data to Cleaned_data solution: Clean_data=>cleaned_datAQ: Using the From django.forms import Form_for_model will appear: Error was:cannot import name Form_for_ Model reason: A new name workaround is used for the model definition of the form in the new version (1.2.1): Reference http://docs.djangoproject.com/en/1.2/topics/forms/modelforms/ From django.forms import modelform## #重新定义class publisherform (modelform): # # #定义元数据 Class Meta:model = publisher##  ############################################################################################################# #第八章 View Focus: URL with named group (R ' articles/(? P&LT;YEAR&GT;\D{4})/$ ', ' year_archive '), # #把year当成形参 (R ' articles/(? P&LT;YEAR&GT;\D{4})/(? P&LT;MONTH&GT;\D{2})/$ ', ' month_archive '), # Year and month are all definitions of formal parameter functions: Mydjango.views in # year Archivedef year_archive ( Request,year): HTML = ' Current year is: ', year return HttpResponse (HTML) # month Archivedef month_archive (Request,mon Th,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, the named groups and A non-named group 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 URL does not match exactly as you expected if anyNamed groups, Django ignores non-named groups and uses named groups directly. § 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 Handbook Learn Notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.