马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 芊芊 于 2018-5-6 21:14 编辑
目录结构如上所示。无法提交表单,折腾了半天就是不行,我也是醉了,提交后,页面停留在http://127.0.0.1:8000/blog/edit/action/ 不会跳转到首页,数据也没有写进数据库。。。
求大神
urls.pyurlpatterns = [
url(r'^, views.index),
url(r'^article/(?P<article_id>[0-9]+), views.article_page,name='article_page'),
url(r'^edit/', views.edit_page, name='edit_page'),
url(r'^edit/action/, views.edit_action, name='edit_action'),
]
views.py# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render
import models
from django.http import HttpResponse
# Create your views here.
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})
edit_page.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Edit Page</title>
</head>
<body>
<form action="{% url 'blog: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>
</html>
|