点击新文章提交之后出现ValueError at /blog/edit/action

django:2.1.0  pycharm:2018.2



invalid literal for int() with base 10: ''
Request Method:POST
Request URL:http://127.0.0.1:8000/blog/edit/action
Django Version:2.1.1
Exception Type:ValueError
Exception 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 965
Python Executable:C:\Users\89758\AppData\Local\Programs\Python\Python37\python.exe
Python Version:3.7.0
Python 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:

<!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 %}
    {% 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="" >
        <label>文章标题
            <input type="text" name="title" value="">
        </label>
        <br>
        <label>文章内容
            <input type="text" name="content" value="">
        </label>
        <br>
 {% endif %}
    <input type="submit" value="提交">
</form>
</body>
</html>


views:

from django.shortcuts import render
from . import models
from django.http import HttpResponse
from 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})


qq_我的愿望是世界和平丶_04084591
浏览 1134回答 2
2回答
打开App,查看更多内容
随时随地看视频慕课网APP