How to create your own website using Python framework Django

  How to create your own website using Python framework Django

  Django station is divided into four steps: 1. Create a Django project, 2, the web page template ported to Django project, 3, data exchange, 4, database

  1 Create a Django project

  I am using PyCharm editor, open PyCharm, clicking on the left below position of the red box, select the terminal, then in the terminal window

  django-admin.py startproject myproject

  Then enter

  python manage.py startapp myapp

  Myproject and above all their own definition myapp project name and the name of APP

  At this point, Django project has been set up.

  2 web template ported to Django project

  1, the new file in myapp two folders, static, and templates, static folder to place css page template, js and other folders, templates folder only put .html file

  2, four Django project file to be modified, because the template ported to Django project, you need to make some modifications in the template file format.

  setting.py

  Add your own APP inside, add a template folder paths

  'DIRS': [os.path.join(BASE_DIR,'templates')],

  url.py

  Added the following statement

  from django.urls import path

  from django.conf import settings

  from django.conf.urls.static import static

  from myapp import views

  Then add the path of the page

  path('login', views.login),

  

Here Insert Picture Description

 

  views.py

  Xx.html same prefix name defines a response function, the function name must be a web page

  def index(request):

  return render(request, 'index.html',

  xx.html

  Next add the following statement to indicate the following template taken over by Django

  {% load static %}

  The following are some examples of statements in the format Django

  {% for line in data1 %}

  {% if line.user == data2.user %}

  {% if line.psw == data2.psw %}

  {% endif %}

  {% endif %}

  {% endfor %}

  As can be seen in the path for loop statement to be used for determining if {%}%.

  3 data exchange

  views.py

  First, the definition of a list, such as user_list [], for storing data

  The method of data POST, so add a statement determines the response function

  def login(request):

  if request.method == "POST":

  username = request.POST.get ( "username", None) // save the user entered data into a variable

  password = request.POST.get("password", None)

  user_list = {"user": username, "psw": password}

  return render (request, 'login.html', { "data1": user_list}) // send data back to the page

  xx.html Wuxi gynecological hospital Which http://mobile.wxbhnkyy39.com/

  After the data is returned to the page read for loop

  {% for line in data1 %}

  line.user = data1.user

  line.psw = data1.psw

  {% endfor %}

  Operation 4 database

  We need to change the three documents

  setting.py

  Django has its own built-in database

  DATABASES = {

  'default': {

  'ENGINE': 'django.db.backends.sqlite3',

  'NAME': os.path.join (BASE_DIR, 'db.sqlite3'), #Django own database

  }

  }

  models.py

  Create models

  class UserInfo(models.Model):

  user = models.CharField(max_length=32)

  psw = models.CharField(max_length=32)

  As long as the move models.py, you must enter the following two statements in the terminal

  python manage.py makemigrations

  python manage.py migrate

  views.py

  Added the following statement

  from myapp import models

  Data is written to the database

  models.UserInfo.objects.create(user="zhoupeng", psw="123456")

  Reading database data

  user_list = models.UserInfo.objects.all()

  It can be read directly in a Web page database operation

  Add the following statement in the file admin.py

  from myapp.models import UserInfo

  admin.site.register(UserInfo)

  Then create a superuser

  In the input terminal

  python manage.py createsuperuser

  It can at http: operate the database / / admin in

  At this point, a full Django project is complete.

  Process Reviews modal

  1, the debugging process any bug first look at where he is not a spelling mistake

  2, sometimes debugging for a long time, nothing wrong, but the page is refreshed not come out, but over time it was automatically good, and there may be a problem ports, try another port

  3, the login is clicking submit to jump to the page, there will be wrong page not found, for the method, the data for the post, no data is to get

  4, the difference url.py path ( '', views.login) and path ( 'index', views.index), the former is the url http: / ... / latter http: / ... / index

  5, the error is "Local variable xxx referenced before assignment", is the problem of local and global variables, and functions added in response to global xxx, or directly to the variables defined in a function

Guess you like

Origin www.cnblogs.com/djw12333/p/11654198.html