点击新文章提交之后出现ValueError at /blog/edit/action
django:2.1.0 pycharm:2018.2invalid literal for int() with base 10: ''Request Method:POSTRequest URL:http://127.0.0.1:8000/blog/edit/actionDjango Version:2.1.1Exception Type:ValueErrorException Value:invalid literal for int() with base 10: ''Exception Location:C:\Users\89758\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.1-py3.7.egg\django\db\models\fields\__init__.py in get_prep_value, line 965Python Executable:C:\Users\89758\AppData\Local\Programs\Python\Python37\python.exePython Version:3.7.0Python Path:['D:\\Python\\myblog', 'C:\\Users\\89758\\AppData\\Local\\Programs\\Python\\Python37\\python37.zip', 'C:\\Users\\89758\\AppData\\Local\\Programs\\Python\\Python37\\DLLs', 'C:\\Users\\89758\\AppData\\Local\\Programs\\Python\\Python37\\lib', 'C:\\Users\\89758\\AppData\\Local\\Programs\\Python\\Python37', 'C:\\Users\\89758\\AppData\\Roaming\\Python\\Python37\\site-packages', 'C:\\Users\\89758\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages', 'C:\\Users\\89758\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages\\django-2.1.1-py3.7.egg', 'C:\\Users\\89758\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages\\pytz-2018.5-py3.7.egg']Server time:星期二, 25 九月 2018 02:49:36 +0000
edit_page:
Edit Page {% csrf_token %} {% if article %} 文章标题
文章内容
{% else %} 文章标题
文章内容
{% endif %}
views:
from django.shortcuts import renderfrom . import modelsfrom django.http import HttpResponsefrom django.http import HttpResponseRedirect# Create your views here.def index(request): article = models.Articel.objects.all() return render(request, 'index.html', {'article':article})def article_page(request,article_id): article= models.Articel.objects.get(pk=article_id) return render(request,'article_page.html',{'article':article})def edit_page(request,article_id): if str(article_id)=='0': return render(request,'edit_page.html') article= models.Articel.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('content','CONTENT') article_id = request.POST.get('article_id','0') if article_id == '0': models.Articel.objects.create(title=title, content=content) articles = models.Articel.objects.all() return render(request,'index.html', {'articles': articles}) article = models.Articel.objects.get(pk=article_id) article.title = title article.content = content article.save() return render(request,'article_page.html',{'article':article}) |
免责声明:本内容仅代表回答会员见解不代表天盟观点,请谨慎对待。
版权声明:作者保留权利,不代表天盟立场。
|
|
|
|