Here I learn it is the teacher's station Liu Jiang, the main reason for this is that his version of the new, but also more details
URL to hold back, approximately equal to the reference http://www.liujiangblog.com/
Start the topic:
1. In a command line interface of a terminal inside pycharm
(This point)
2. Enter the command
python manage.py startapp polls
polls voting system is the name of Liu Jiang teacher tutorial created, you can play any name
3. Observe directory on your left you will find more than a new folder, there is also a lot of py file
4. Write view
Here the views.py view of the corresponding file, edit, as follows:
polls/views.py
from django.http import HttpResponse def index(request): return HttpResponse("Hello, world. You're at the polls index.")
Here you know just saying those words inside quotation marks
Configuring the app's url
polls/urls.py
from django.urls import path from . import views urlpatterns = [ path('', views.index, name='index'), ]
Here Remember, urlpatterns name is fixed, do not wrong or else find another burst
Then configure the project url
pysite/urls.py
pysite here is my project is located at, modify according to their own circumstances
from django.contrib import admin from django.urls import include, path urlpatterns = [ path('polls/', include('polls.urls')), path('admin/', admin.site.urls), ]
5. Start View 127.0.0.1:8000/polls
This time you can see.
django can be connected to various databases, the default seems to be called sqllite, the magic that I will learn a python After learning points java, until about the time to understand some of the knowledge of the full stack java, and only then go back and look at this place understand, there will be written into the database connection configuration setting.py, and the maven jar lump sum of about the same thing, then, the last point is expected to do deep CRUD, and only in the configuration when connected mysql django and have a pit, (just climb out)
First you have to modify the setting in
# DATABASES = { # 'default': { # 'ENGINE': 'django.db.backends.sqlite3', # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), # } # } #pysite/setting.py #Database #https://doc.djangoproject.com/en/1.11/ref/settings/#database Import pymysql # here two lines is very important pymysql.install_as_MySQLdb () DATABASES = { 'default':{ 'ENGINE':'django.db.backends.mysql', 'NAME':'myexp', 'HOST':'localhost', 'USER':'root', 'PASSWORD':'*******', 'PORT':'3306', 'OPTIONS': { 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",} } }
Part of the comment is the default, because here I want to connect to a local mysql
Here are several attributes drive, database name, address database host, database user name, password, and port (mysql default 3306), the last few days Research -> ->
I hid it under a password, think about is privacy. I called the database myexp.
After completion of the normal connection, you can try to start django, originally migrated directly, I accidentally found two will be reported the same mistake, I will feel a little safer
The first pit: As for django 5.0 or 3.0 or later seems to have little support issues after the end point of madness tips
This one:
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.3 or newer is required; you have 0.7.11
This is very snakeskin, because the problem is the result base.py
This file contains the sentence: (I commented that)
This comment on the line, and then enter the second layer tunnel
This time you run, then there will be:
'str' object has no attribute 'decode'
This error.
According to locate the file error D: \ pysite \ mysite \ venv \ Lib \ site-packages \ django \ db \ backends \ mysql \ operations.py
(The above is my project)
146 line there is a saying, after reading feeling very angry, always said no strings decode method, the problem lies in. . . Should encode rather than decode, the solution is directly changed
def last_executed_query(self, cursor, sql, params): # With MySQLdb, cursor objects have an (undocumented) "_executed" # attribute where the exact query sent to the database is saved. # See MySQLdb/cursors.py in the source distribution. query = getattr(cursor, '_executed', None) if query is not None: query = query.encode(errors='replace') return query
Ah, so after it's ok
Then back to the terminal is to be migrated your database! ! !
python manage.py migrate
Well, enter your mysql
Discovered several new multi-table, then you are basically completed the task, even on a database regarded Hey.