Basic knowledge of Django

Keywords: Python Django Database MySQL Pycharm

I. Installation and Use

  • Download and install

    • Command line: pip3 install django==1.11.21

    • pycharm

  • Create a project

    • Command line:

      • Find a folder to store project files and open the terminal:

      • Django-admin start project project project name

      • Project catalogue

    • pycahrm

  • start-up

    • command line

      • Switch to manage.py under the project's root directory

      • python36 manage.py runserver - 127.0.0.1:80`

      • python36 manage.py runserver 80-127.0.0.1:80

      • python36 manage.py runserver 0.0.0.0:80-0.0.0.0:80

    • pycharm: point green triangle startup configurable

  • Simple use

    • Example: Return the HTML specified file

    # stay urls.py in
    # Import
    from django.shortcuts import HttpResponse,render
    ​
    # function
    def index(request):
        # return HttpResponse('index')
        return render(request,'index.html')
    ​
    # url Correspondence of sum function
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^index/', index),
    ] 

II. Static Documents

  • To configure

    • Settings in settings.py

    STATIC_URL = '/static/'          # alias
    STATICFILES_DIRS = [            # Set file paths, you can set multiple
        os.path.join(BASE_DIR, 'static1'),
        os.path.join(BASE_DIR, 'static'),
        os.path.join(BASE_DIR, 'static2'),
    ]  
  • Use

    • Add an alias before the path: / static/

    • Multiple file paths, using the same alias, are not file names

    • If the pathname after the alias is the same, look it up in the order of the STATICFILES_DIRS list

    <link rel="stylesheet" href="/static/css/login.css">         {# Alias Beginning #} 

3. Simple login examples

  • Notes on form submission data:

    • Submit address: action = ", request way: method="post"

    • All input boxes have name attributes, such as name="username"

    • There is an input box type="submit" or a button

  • Submit post requests, because there is a csrf check in Django, all requests will go wrong

    • Solution: Annotate'django.middleware.csrf.CsrfViewMiddleware'of MIDDLEWARE in settings

  • redirect

    • Import: from django.shortcuts import redirect

    • Usage

    # Use in functions,for example
    return redirect('/index/')      # Parameter: Path url
    # Note: The preceding must be added/,Representatives from url Root splicing, otherwise it will be in the present url Stitching at the back
    from django.shortcuts import HttpResponse, render, redirect
    ​
    def index(request):
        # return HttpResponse('index')
        return render(request, 'index.html')
    ​
    def login(request):
        if request.method == 'POST':
            # Obtain form Books submitted on forms
            username = request.POST['username']
            password = request.POST['password']
            # Verify username and password
            if models.User.objects.filter(username=username,password=password):
                # Verify successful jump to index page
                # return redirect('https://www.baidu.com/')
                return redirect('/index/')
            # Unsuccessful re-login
        return render(request, 'login.html')
    ​
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^index/', views.index),
        url(r'^login/', views.login),
    ]
    Example

IV. app

5. Using MySQL Process

VI. MVC and MTV

Posted by jerastraub on Fri, 09 Aug 2019 05:24:25 -0700