每次刷新 http://127.0.0.1:8000/blog/edit/action/ 都会创建一篇文章,有人遇到这个问题吗
每次刷新这个界面,都会创建一篇文章,有人遇到这个问题吗
每次刷新这个界面,都会创建一篇文章,有人遇到这个问题吗
2019-05-25
我用的python3.73
各种测试用了好几个小时,终于成功做出了视频中的几个跳转的功能,文件分享给大家:
index.html
<body> <h1> <a href="{% url 'edit_page' %}">新文章</a> </h1> {% for article in articles %} <h3> <a href="/blog/article/{{ article.id }}">{{ article.title }}</a> </h3> {% endfor %} </body>
edit_page.html:
<body> <form action="{% url 'edit_action' %}" method="post"> {% csrf_token %} <label> <input type="text" name="title"/> </label> <br> <label> <input type="text" name="content"/> </label> <br> <input type="submit" value="提交"> </form> </body>
urls.py:
urlpatterns = [ path(r'', views.index), path('article/<int:article_id>', views.article_page), path('edit/', views.edit_page, name='edit_page'), path('edit/action', views.edit_action, name='edit_action'),
views.py:
def index(request): # article = models.Article.objects.get(pk=1) 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})
真是不容易啊,前期的课程听起来很顺,到后边各种不致就都出来了!
举报