urls
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('App.urls'))
]
App.urls
from django.urls import path
from App import views
app_name = 'App'
urlpatterns = [
path('', views.index, name='index'),
path('register/', views.RegisterView.as_view(a=90), name='register'),
path('template/',views.MyTemplateView.as_view(),name='template'),
path('list/',views.UserListView.as_view(), name='list'),
path('detail//',views.UserDetailView.as_view(), name='detail'),
path('create/',views.UserCreateView.as_view(),name='create'),
path('publish/',views.ArticlePulishView.as_view(),name='publish'),
path('demo/',views.DemoView.as_view(),name='demo')
]
App.views
from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.utils.decorators import method_decorator
from django.views.generic import View
from django.views.generic import TemplateView, ListView, DetailView,CreateView
from App.models import User
def index(request):
return HttpResponse("首页")
class RegisterView(View):
a = 10
def get(self,request):
print(self.a)
return HttpResponse("GET")
def post(self,request):
return HttpResponse("POST")
def put(self,request):
return HttpResponse("PUT")
def delete(self,request):
return HttpResponse("DELETE")
class MyTemplateView(TemplateView):
template_name = 'example.html'
def get_context_data(self, **kwargs):
kwargs = super().get_context_data(**kwargs)
kwargs['name'] = '意大利'
kwargs['title'] = "欧洲新冠王者"
return kwargs
class UserListView(ListView):
template_name = 'userlist.html'
paginate_by = 1
ordering = ("uid",)
def get_queryset(self):
data = User.objects.filter(uid__gt=20)
ordering = self.get_ordering()
if ordering:
if isinstance(ordering, str):
ordering = (ordering,)
data = data.order_by(*ordering)
return data
class UserDetailView(DetailView):
template_name = 'userdetail.html'
queryset = User.objects.all()
context_object_name = 'user'
class UserCreateView(CreateView):
def get(self,request):
return render(request,'userregister.html')
def post(self,request):
data = request.POST.dict()
data.pop('csrfmiddlewaretoken')
User.objects.create(**data)
return redirect("/")
def check_login(func):
def inner(request,*args,**kwargs):
if request.session.get('usename'):
return func(request,*args,**kwargs)
return redirect("/")
return inner
@method_decorator(check_login,name='dispatch')
class ArticlePulishView(View):
def get(self,request):
return HttpResponse("文章展示")
def post(self,request):
return HttpResponse("发表文章")
class DemoView(View):
def get(self,request):
object_list = User.objects.all()
pagitor = Paginator(object_list,10)
pager = pagitor.page(1)
object_list = pager.object_list
return render(request,'userlist.html',locals())
App.models
from django.db import models
class User(models.Model):
uid = models.AutoField(primary_key=True)
username = models.CharField(unique=True, max_length=30)
password = models.CharField(max_length=128)
regtime = models.DateTimeField(auto_now=True)
sex = models.IntegerField(blank=True, null=True)
is_active = models.IntegerField(blank=True, null=True)
class Meta:
db_table = 'user'
example.html
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ title }}title>
head>
<body>
<p>
{{ name }}
p>
body>
html>
userlist.html
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<table border="1" cellspacing="0" width="80%">
{% for user in object_list %}
<tr>
<td>{{ user.uid }}td>
<td>{{ user.username }}td>
<td>{{ user.password }}td>
tr>
{% endfor %}
table>
<div>
{% for current in paginator.page_range %}
<a href="">{{ current }}a>
{% endfor %}
div>
body>
html>
userdetail.html
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<h2>用户信息h2>
{{ user.uid }} ---{{ user.username }}----{{ user.password }}
body>
html>
userregister.html
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<form action="{% url 'App:create' %}" method="post">
{% csrf_token %}
用户名: <input type="text" name="username"> <br>
密码: <input type="password" name="password"> <br>
<input type="submit">
form>
body>
html>
App.tests
class Dog:
name = '黑背'
def run(self):
print("run")
def go():
print("GO")
dog = Dog()
fun = getattr(dog,'run1',go)
fun()