程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

The basic process of django project

編輯:Python
  1. Create django project

    django-admin startproject project name

  2. Create django application

    django-admin startapp app name

  3. Configuration file---in the settings.py file in the directory with the same name as the project

    • Register app

    • Database

    • Configuration Template Directory

      • Modify DIRS in TEMPLATES in settings.py

        # Splicing the root directory and the created template file template is a template folder created by yourself, storing html filesof'DIRS': [os.path.join(BASE_DIR,'template')],
  4. Write model class----write in models.py in app

    from django.db import models# Define the book model classclass Book(models.Model):# Title Price Quantity Aliasname = models.CharField(max_length=30,verbose_name="book title")# Up to 5 digits of which 2 are decimalsprice = models.DecimalField(max_digits=5,decimal_places=2,verbose_name="price")# IntegerField cannot write max_lengthnum = models.IntegerField(verbose_name="number")# define metaclassclass Meta:verbose_name_plural = "Books"db_table = "book" # Specify the generated table namedef __str__(self):# Return the description of the objectreturn self.name

  5. Migration

    • Generate migration files

      python manage.py makemigrations

    • Execute migration file

      python manage.py migrate

  6. Create superuser python manage.py createsuperuser

  7. Register model class---admin.py

    Register model class in admin.pyadmin.site.register(model class)from .models import Student,Teacheradmin.site.register(Student,Teacher)

  8. Write View

    • Define view function

      # Display single student information String parameterdef detailStudent(request):# select * from table where id = 1; model class.objects.get(condition)# request.GET: is the request method get(): the method of obtaining data# id = request.GET.get('id')name = request.GET.get('name')# Only query data with id 1# student = Student.objects.get(id=id)student = Student.objects.get(name=name)return render(request,'detail.html',{"student":student})

  9. Configure routing

    Route distribution

    • To create a subrouting file

    • Configure the child routing file in the main routing file

    • Configure routes in sub-route files

  10. Start project access

    python manage.py runserver


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved