django3 典型代碼

views.py

from django.shortcuts import render
from login import forms

# Create your views here.

def register(request):
    if request.method == "POST":
        user_form = forms.UserForm(request.POST)
        if user_form.is_valid():
            message = "informations are saved!"
            user_form.save()
        else:
            message = "something is wrong!"
    else:
        user_form = forms.UserForm()
    return render(request, 'about.html', locals())

models.py

# -*- encoding: utf-8 -*-
from django.db import models

# Create your models here.

class User(models.Model):
    nickname    = models.CharField(max_length = 32)
    mobile      = models.CharField(max_length = 16)
    email       = models.EmailField(max_length = 254, blank = True)
    address     = models.CharField(max_length = 254, blank = True)
    createtime  = models.DateTimeField(auto_now_add = True)
    updatetime  = models.DateTimeField(auto_now = True)

    def __str__(self):
        return self.mobile

class ExtraMobile(models.Model):
    user        = models.ForeignKey(User, on_delete=models.CASCADE)
    mobile      = models.CharField(max_length = 16)

    def __str__(self):
        return self.mobile

forms.py

from django.forms import ModelForm
from login import models

class UserForm(ModelForm):
    class Meta:
        model   = models.User
        fields  = "__all__"

class ExtraMobileForm(ModelForm):
    class meta:
        model   = models.ExtraMobile
        fields  = "__all__"
~                             

base.html

<!--base.html-->
{% load static %}
<!DOCTYPE html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>test website</title>
    <link rel="stylesheet" href="{% static "bootstrap.min.css" %}">
    <link rel="stylesheet" href="{% static "glyphicon.css" %}" />
    <link rel="stylesheet" href="{% static "animate.css" %}" />
    <link rel="stylesheet" href="{% static "swiper.min.css" %}" />
    {% block css %} {% endblock %}
    <script src="{% static "jquery-3.3.1.min.js" %}"></script>
    <script src="{% static "swiper.min.js" %}"></script>
    <script src="{% static "bootstrap.min.js" %}"></script>
    <script src="{% static "popper.min.js" %}"></script>
    <script src="{% static "animate.js" %}"></script>
    {% block jsheader %} {% endblock %}
</head>
<body>
        {% include 'header.html' %}
        {% block content %}     {% endblock %}
        {% include 'footer.html' %}
        {% block jsfooter %} {% endblock %}
</body>
</html>

main.html

{% extends 'base.html' %}
{% load static %}
{% block css %}<link rel="stylesheet" href="{% static "css/about.css" %}" />{% endblock %}
{% block content %}
{% if message %}
        <div class="alert alert-warning">{{ message }}</div>
{% endif %}
        <form name="myform" method="post">
                {% csrf_token %}
                <table>
                        {{ user_form.as_table }}
                </table>
                <input type="submit" value="submit">

        </form>
{% endblock %}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章