• Django 基本设置


    建立django目录,为了独立区分app和主站的关系,需要把app完全和主站分离

    app/views.py

    from django.shortcuts import render
    from django.http import HttpResponseRedirect,HttpResponse
    import os
    from app.forms import MomentForm
    from django.core.urlresolvers import reverse
    from django.shortcuts import render
    
    # Create your views here.
    def moments_input(request):
        if request.method =='POST':
            form = MomentForm(request.POST)
            if form.is_valid():
                moment = form.save()
                moment.save()
                return HttpResponseRedirect(reverse('welcome'))#reverse连接urls内的name值,来进行反向查找网址
        else:
            form = MomentForm()
        PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        return render(request,os.path.join(PROJECT_ROOT,'app/templates','moments_input.html'),{'form':form}) #连接到模版文件路径
    
    def welcome(request):
    
        return HttpResponse('HELLO')

    app/urls.py管理app中的url映射

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from django.conf.urls import url
    from . import views
    
    urlpatterns = [
        url(r'moments_input',views.moments_input),
        url(r'wc',views.welcome,name='welcome'),# 添加name值,以便于reverse查找
    ]

    django/urls.py

    from django.conf.urls import url
    from django.contrib import admin
    from django.conf.urls import include
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^app/', include('app.urls')), # 建立与app/urls的路由建立连接关系
    ]

    app.models.py 建立模型

    # _*_ coding: utf-8 _*_
    from django.db import models
    
    # Create your models here.
    
    KIND_CHOICES = (
        ('python技术','python技术'),
        ('数据库技术','数据库技术'),
        ('经济学','经济学'),
        ('文体资讯','文体资讯'),
        ('个人心情','个人心情'),
        ('其他','其他'),
    )
    
    class Moment(models.Model):
        content = models.CharField(max_length=200)
        user_name = models.CharField(max_length=20,default='匿名')
        kind = models.CharField(max_length=20,choices=KIND_CHOICES,default=KIND_CHOICES[0])

    定义表单类app/form.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from django.forms import ModelForm
    from app.models import Moment
    
    class MomentForm(ModelForm):
        class Meta:
            model = Moment  # 连接数据库与表单的关系
            fields = '__all__' # 导入所有字段

    创建模版文件app/templates/moments_input.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>消息输入页面</title>
    </head>
    <body>
    <form action="?" method="post">
        <fieldset>
            <legend>请输入并提交</legend>
            {{ form.as_p }}
            <input type="submit" value="submit"/>
        </fieldset>
    </form>
    </body>
    </html>

    使用自带管理界面app/admin.py

    from django.contrib import admin
    
    # Register your models here.
    from .models import Moment
    
    admin.site.register(Moment)

  • 相关阅读:
    Win10 rocketmq 安装
    Vulnhub DC7
    Vulnhub DC6
    Vulnhub DC5
    Vulnhub DC4
    Vulnhub DC3
    一文带你学会CSRF漏洞
    Shiro反序列化漏洞复现与实战
    DataPipeline CPO 陈雷:实时数据融合之法:便捷可管理
    DataPipeline合伙人 & CPO 陈雷:企业实时数据管理问题与实践 | 附PPT下载
  • 原文地址:https://www.cnblogs.com/Erick-L/p/7094242.html
Copyright © 2020-2023  润新知