无法完成数据添加和页面跳转。Django2.1.7,python是3.6.5
edit_page
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Edit page</title> </head> <body> <form action="{% url 'blog:edit_action'}" method="post"> <label>文章标题 <input type="text" name="title"/> </label> <br/> <label>文章内容 <input type="text" name="content"/> </label> <br/> <input type="submit" value="提交"> </form> </body> </html>
views.py
from django.shortcuts import render from django.http import HttpResponse from . import models def index(request): articles = models.Article.objects.all() return render(request, 'blog/index.html', {'articles': articles}) def article_page(request,article_id): article= models.Article.objects.get(pk= article_id) return render(request, 'blog/article_page.html', {'article':article}) def edit_page(request): return render(request, 'blog/edit_page.html') def edit_action(request): title = request.post.get('title', 'TITLE') content = request.post.get('content', 'CONTENT') models.Article.objects.create(title=title, content=content) articles = models.Article.objects.all() return render(request, 'blog/index.html', {'articles': articles})
urls.py
from django.urls import path,include from. import views app_name = 'blog' urlpatterns = [ path('index/', views.index), path('article/<int:article_id>', views.article_page, name='article_page'), path('edit/', views.edit_page, name='edit_page'), path('edit/action', views.edit_action, name='edit_action'), ]