标签:return ext lan 列表 tor 提交 文章 方式 time
from django.views.decorators.http import require_http_methods
from django.http import HttpResponse
from django.shortcuts import render
from .models import Article
@require_http_methods(['GET', 'POST'])
def index2(request):
<!--首先判断客户端发送的是哪种请求GET OR POST-->
<!--注意这里一定要是“==” 只有“==”才会返回True or False-->
if request.method == 'GET':
return render(request,'static/add.html')
else:
title = request.POST.get('title')
content = request.POST.get('content')
price = request.POST.get('price')
Article.objects.create(title=title, content=content, price=price)
articles = Article.objects.all()
return render(request, 'static/index.html', context={'articles': articles}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table>
<thead>
<tr style="width: 20px">标题</tr>
<tr style="width: 20px">内容</tr>
<tr style="width: 20px">价格</tr>
<tr style="width: 20px">创建时间</tr>
</thead>
<tbody>
{% for article in articles %}
<tr>
<td><a href="#">{{ article.title }}</a></td>
<td><a href="">{{ article.content }}</a></td>
<td><a href="">{{ article.price }}</a></td>
<td>{{ article.create_time }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
from django.urls import path
from article import views
urlpatterns = [
path('', views.index, name='index'),
path('add/', views.add, name='add'),
path('add2/', views.index2, name='add2'),
]
from django.views.decorators.http import require_GET
@require_GET
def index(request):
articles = Article.objects.all()
return render(request, 'static/index.html', context={'articles':articles})
<ul>
{% for article in articles %}
<li>{{% article.title %}}</li>
<li>{{% article.content %}}</li>
<li>{{% article.price %}}</li>
{% endfor %}
</ul>
from django.views.decorators.http import require_POST
@require_POST
def add(request):
title = request.POST.get('title')
content = request.POST.get('content')
Article.objects.create(title=title, content=content)
return HttpResponse('success')
标签:return ext lan 列表 tor 提交 文章 方式 time
原文地址:https://www.cnblogs.com/guyan-2020/p/12300526.html