不能提交数据
为什么这一节里面我按照视频敲的代码,不能提交内容啊。 提示 ValueError at /blog/edit/action/ invalid literal for int() with base 10: 'article.id'
为什么这一节里面我按照视频敲的代码,不能提交内容啊。 提示 ValueError at /blog/edit/action/ invalid literal for int() with base 10: 'article.id'
2018-06-06
这是提示错误信息
from django.shortcuts import render from django.http import HttpResponse from . import models # Create your views here. def index(request): articles = models.Article.objects.all() return render(request,'index.html',{'articles':articles}) def article_page(request,article_id): article = models.Article.objects.get(pk=article_id) return render(request,'article_page.html',{'article':article}) def edit_page(request,article_id): if article_id == '0': return render(request,'edit_page.html') article = models.Article.objects.get(pk=article_id) return render(request,'edit_page.html',{'article':article}) def edit_action(request): title = request.POST.get('title','TITLE') content = request.POST.get('conetnet','CONTENT') article_id = request.POST.get('article_id','0') if article_id == '0': models.Article.objects.create(title=title,conetnt=content) articles = models.Article.objects.all() return render(request, 'index.html', {'articles': articles}) article = models.Article.objects.get(pk = article_id) article.title = title article.content = content article.save() return render(request, 'article_page.html', {'article': article})
这是views.py的代码
urlpatterns = [ url(r'^index/$', views.index), url(r'^article/(?P<article_id>[0-9]+)/$', views.article_page,name='article_page'), url(r'^edit/(?P<article_id>[0-9]+)/$', views.edit_page,name='edit_page'), url(r'^edit/action/$', views.edit_action,name='edit_action'), ]
这是blog/urls.py的代码
<body> <form action="{% url 'blog:edit_action' %}" method="post"> {% csrf_token %} {% if article %} <input type="hidden" name="article_id" value="article.id"> <label>文章标题 <input type="text" name="title" value="{{ article.title }}"> </label> <br/> <label>文章内容 <input type="text" name="content" value="{{ article.content }}"> </label> <br/> {% else %} <input type="hidden" name="article_id" value="0"> <label>文章标题 <input type="text" name="title" > </label> <br/> <label>文章内容 <input type="text" name="content" > </label> {% endif %} <br/> <input type="submit" value="提交"> </form> </body>
这是edig_page.html的代码
举报