为什么引发ValueError异常?

时间:2018-10-01 18:28:47

标签: python django

models.py

c = {
    '# 2.537'    '1.219'      '0.457'    '0.214'    '# 0.120'    '0.245'
    '3.244'      '# 1.400'    '0.649'    '0.515'    '0.207'      '0.075'
    '2.993'      '1.282'      '0.605'    '0.309'    '0.140'      '0.140'
    '3.278'      '1.507'      '0.885'    '0.405'    '0.160'      '0.111'
    '3.691'      '1.474'      '0.650'    '0.316'    '0.180'      '0.155'}

IndexE = strfind(c, '#')

data=[
    0.8147    0.0975    0.1576    0.1419    0.6557    0.7577
    0.9058    0.2785    0.9706    0.4218    0.0357    0.7431
    0.1270    0.5469    0.9572    0.9157    0.8491    0.3922
    0.9134    0.9575    0.4854    0.7922    0.9340    0.6555
    0.6324    0.9649    0.8003    0.9595    0.6787    0.1712]

[row,col] = find(~cellfun(@isempty,IndexE))

temp=strsplit(num2str(data(:)'))
shape=size(data)
temp2 = reshape(temp, shape(1,1), shape(1,2))
dataT1_2=temp2;

for m = 1:length(row)
    temp3(m,1)=strcat('# ',temp2(row(m,1),col(m,1)));
    dataT1_2(row(m,1),col(m,1))=temp3(m,1);
end

views.py

class Post(models.Model):
    message = models.CharField(max_length=2000)
    topic = models.ForeignKey(Topic, related_name="posts", null=True, 
               blank=True, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)
    created_by = models.ForeignKey(User, null=True, blank=True, 
                    on_delete=models.CASCADE)
    like = models.IntegerField(default=0)
    views = models.IntegerField(default=0)

class ReplyPost(models.Model):
    reply = models.CharField(max_length=2000)
    post = models.ForeignKey(Post, related_name="reply", null=True, 
            blank=True, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)
    created_by = models.ForeignKey(User, null=True, blank=True, 
                    on_delete=models.CASCADE)

urls.py

def reply_post(request, pk):
    post = get_object_or_404(Post, pk=pk)
    if request.method == "POST":
        form = ReplyPostForm(request.POST)

        if form.is_valid():
            replypost = form.save(commit = False)
            replypost.reply = form.cleaned_data.get("reply")
            replypost.post = post
            replypost.created_by = request.user
            replypost.save()
            return redirect("post", post.id)

    else:
        form = ReplyPostForm()
        args = {
            'form': form,
            'post':post
         }
        return render(request, 'home/replypost.html', args)

forms.py

urlpatterns = [
   #.....

   url(r'^home/questions/(?P<pk>\d+)/reply/$', views.reply_post, 
         name="replypost"),
   url(r'^home/questions/(?P<pk>\d+)/$', views.post, name="post"),
]

错误:

class ReplyPostForm(ModelForm):
    reply = forms.CharField(widget = forms.Textarea)

    class Meta:
        model = ReplyPost
        fields = ('reply',)

我无法理解该代码出了什么问题。如果上述代码不足以理解,请更新。

1 个答案:

答案 0 :(得分:1)

对于请求为HttpResponse但格式无效的情况,您的代码需要返回一个POST实例。

查看您的代码,尝试将最后几行移回一个缩进级别;这样,所有非POST形式的有效表单都将落入最后一个return语句。

def reply_post(request, pk):
    post = ...

    if request.method == "POST":
        form = ReplyPostForm(request.POST)

        if form.is_valid():
            ...
            return redirect("post", post.id)
    else:
        form = ReplyPostForm()

    args = {
        'form': form,
        'post': post,
    }
    return render(request, 'home/replypost.html', args)