Getting Started with Django - what you want "Python programming from entry to practice" Chapter 18, the next step of recording a rough and stepped pit

**** yourself as a beginner, felt that these pits occur primarily for two reasons:
low Django version 1. The original book with, now mostly filled with Django2.0, some of which changed the grammar, so the original book Programming even if there will be a lot of good word copy being given
weak base 2. this is not to say.

1. Install virtualenv

The book says anaconda3, the concept of a pip install virtualenv, I just started with did not understand, fiddle for a long time from the anaconda prompt, enter cmd later found in the terminal installation.

2. Establish a virtual environment

The book says, "In the terminal change to the directory", this also checked for a long time, later found to find a suitable place to create a new folder, the mouse into this folder, hold down shift and right mouse button, enter cmd on the line, and then python -m venv 11_env get away. 11_env which is the name of the environment can be changed.

3. Activate the virtual environment

In the windows, so use the "11_env \ Scripts \ activate" is a virtual environment where 11_env name, took himself a name on the line. Will become (11_env) before entering a prompt After activation, the addition of a parentheses activated.

4. Start the server

After python manage.py runserver to start the server, if you modify a file, the server will display real-time status, including error or something.

5. Build URL mapping

References: https: //docs.djangoproject.com/en/3.0/topics/http/urls/

5.1 Naming namespace problems

Operation Note error: Specifying a namespace in include () withou providing an app_name
previous wording

urlpatterns = [
    path('admin/', admin.site.urls),
    url(r"^users/", include("users.urls", namespace='aaa')),
]

`Now the wording, include the first argument needs to be a two-dimensional Ganso

urlpatterns = [
    path('admin/', admin.site.urls),
    url(r"^users/", include(("users.urls", 'users'), namespace='aaa')),
]

5.2 URL in the '^ $' regular expression problem

在运行服务器时console提示报错:
“WARNINGS:?: (2_0.W001) Your URL pattern ‘^index’ has a route that contains ‘(?P<’, begins with a ‘^’, or ends with a ‘$’. This was likely an oversight when migrating to django.urls.path().”

See the original solution to this link: https: //blog.csdn.net/SilentWu/article/details/88205831
because From Django2.0, urls.py configure different
solution: url function into path, NA ^ , $ route as
the original:

urlpatterns = [
    #主页
    path('^$', views.index,name='index'),
    
]

right now:

urlpatterns = [
    #主页
    path('', views.index,name='index'),
    
]

5.3 'topics / (? P <topic_id> \ d +) /' problem (P378, the first 18.4.3)

在运行Django过程中,服务器提示:
“WARNINGS:?: (2_0.W001) Your URL pattern ‘^index’ has a route that contains ‘(?P<’, begins with a ‘^’, or ends with a ‘$’. This was likely an oversight when migrating to django.urls.path().”

This problem checked quite a while to resolve. The front end of the original dynamic request path, URLS re_path need to import configuration prior to use.
Original Code (and path to remove the url ^ $):

urlpatterns = [
    #主页
    path('', views.index,name='index'),
    #显示所有的主题
    path('topics/',views.topics,name='topics'),
    #特定主题的详细页面
    path('topics/(?P<topic_id>\d+)/',views.topic,name='topic'),
]

After changing the code:

urlpatterns = [
    #主页
    path('', views.index,name='index'),
    #显示所有的主题
    path('topics/',views.topics,name='topics'),
    #特定主题的详细页面
    re_path('topics/(?P<topic_id>\d+)/',views.topic,name='topic'),
]

Of course, do not forget to import re_path, otherwise will be error, hey hey hey.

from django.urls import path,re_path

This would solve.

6. Set up a user account

6.1 no module named 'django.core.urlresolvers' problem

The solution is: from django.urls import reverse
appears An unexpected error recently migrated from django1.9 to django2.0, the reason is: django2.0 the original django.core.urlresolvers package change for django.urls packets, we need to import packages have changed a bit on it.

6.2form=EntryForm(data=request.POST) invalid character in identifier问题

The reason is found, there is a Chinese character, dizzy. . . . . . .
Add a checked useful links for your reference: https: //www.jianshu.com/p/d53680ccbf50

Released eight original articles · won praise 3 · Views 1941

Guess you like

Origin blog.csdn.net/youtaidudewamao/article/details/104378642