我正在尝试创建社交迷你社交网络项目的Feed部分。我有一个"饲料"处理所有用户帖子的应用程序。一切正常,除非我在用户post_form页面上提交帖子,我收到此错误:
ValueError at /feed/new/
invalid literal for int() with base 10: 'User'
我不确定问题是什么,但这是我的代码。回溯并不会突出显示任何内容,但如果你想看到它,请告诉我。
Feed app urls:
from django.conf.urls import url
from feed import views
app_name = 'feed'
urlpatterns = [
url(r'^new/$',views.CreatePostView.as_view(),name='new_post'),
url(r'^post/(?P<pk>\d+)/edit/$',views.UpdatePostView.as_view(),name='edit_post'),
url(r'^post/(?P<pk>\d+)/delete/$',views.DeletePostView.as_view(),name='delete_post'),
url(r'^post/(?P<pk>\d+)/comment/$',views.add_comment_to_post,name='add_comment_to_post'),
]
Feed应用视图:
from django.shortcuts import render,get_object_or_404,redirect
from django.utils import timezone
from feed.models import UserPost,UserComment
from feed.forms import PostForm,CommentForm
from django.urls import reverse_lazy
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import (TemplateView,ListView,
DetailView,CreateView,
UpdateView,DeleteView)
# Create your views here.
##Posts Views
class HomeView(LoginRequiredMixin,ListView):
login_url = 'login'
model = UserPost
ordering = ['-post_date']
class CreatePostView(LoginRequiredMixin,CreateView):
login_url = 'login'
redirect_field_name = '/userpost_list.html'
form_class = PostForm
model = UserPost
class UpdatePostView(LoginRequiredMixin,UpdateView):
login_url = 'login'
redirect_field_name = '/userpost_detail.html'
form_class = PostForm
model = UserPost
class DeletePostView(LoginRequiredMixin,DeleteView):
model = UserPost
success_url = reverse_lazy('userpost_list')
##Comments Views
def add_comment_to_post(request,pk):
post = get_object_or_404(UserPost,pk=pk)
if request.method == 'POST':
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.post = post
comment.save()
return redirect('userpost_list',pk=userpost.pk)
else:
form = CommentForm()
return render(request,'feed/userpost_list.html',{'form':form})
不确定您是否需要此功能,但这里是Feed应用程序表单:
from django import forms
from feed.models import UserPost,UserComment
class PostForm(forms.ModelForm):
class Meta():
model = UserPost
fields = ('title','post_body')
widget = {
'title':forms.TextInput(attrs={'class':'post-title'}),
'post_body':forms.Textarea(attrs={'class':'post-body'}),
}
class CommentForm(forms.ModelForm):
class Meta():
model = UserComment
fields = ('comment_body',)
widgets = {
'comment_body':forms.TextInput(attrs={'class':'comment-body'}),
}
Feed应用模型:
from django.db import models
from django.core.urlresolvers import reverse
from django.conf import settings
from django.contrib.auth import get_user_model
User = get_user_model()
# Create your models here.
class UserPost(models.Model):
author = models.ForeignKey(User,related_name='userpost',default='User')
post_date = models.DateTimeField(auto_now_add=True)
title = models.CharField(max_length=150,blank=False)
post_body = models.TextField(max_length=1000,blank=False)
def publish(self):
self.save()
def get_absolute_url(self):
return reverse('index')
def __str__(self):
return self.title
class UserComment(models.Model):
post = models.ForeignKey('feed.UserPost',related_name='comments')
author = models.ForeignKey(User,related_name='usercomment')
comment_date = models.DateTimeField(auto_now_add=True)
comment_body = models.TextField(max_length=500)
def publish(self):
self.save()
def get_absolute_url(self):
return reverse("userpost_list")
def __str__(self):
return self.text
答案 0 :(得分:0)
问题出在您的模型中 - 字符串'User'
不是有效用户。
class UserPost(models.Model):
author = models.ForeignKey(User, related_name='userpost', default='User')
最简单的解决方法是删除默认值:
class UserPost(models.Model):
author = models.ForeignKey(User, related_name='userpost')
但是,除非您设置null=True
,否则您的CreatePostView
会收到错误,因为您在创建帖子时未设置用户。您可以通过覆盖form_valid
并在保存表单之前将作者设置为self.request.user
来解决此问题:
class CreatePostView(LoginRequiredMixin,CreateView):
login_url = 'login'
redirect_field_name = '/userpost_list.html'
form_class = PostForm
model = UserPost
def form_valid(self, form):
form.instance.author = self.request.user
return super(CreatePostView, self).form_valid(form)