在单击后提交按钮时,就像创建对象成功一样,但是像字段正在更新为空值,而django则收到错误302。有人可以帮我这个忙。
我已经从 SongVoteUpdateView 中删除了 form_valid 函数,现在更新部分工作正常,但是如果我从 SongVoteCreatView中删除了 form_valid 函数, / strong>会引发类似NOT NULL constraint failed: album_vote.song_id
的虚假错误。 For trace back please refer this link,如果未删除,将创建空白的空白对象。
models.py中的代码
class VoteManager(models.Manager):
def get_vote_or_unsaved_blank_vote(self,song,user):
try:
return Vote.objects.get(song=song,user=user)
except ObjectDoesNotExist:
return Vote(song=song,user=user)
class Vote(models.Model):
UP = 1
DOWN = -1
VALUE_CHOICE = ((UP, "?️"),(DOWN, "?️"),)
like = models.SmallIntegerField(null=True, blank=True, choices=VALUE_CHOICE)
user = models.ForeignKey(User,on_delete=models.CASCADE)
song = models.ForeignKey(Song, on_delete=models.CASCADE)
voted_on = models.DateTimeField(auto_now=True)
objects = VoteManager()
class Meta:
unique_together = ('user', 'song')
views.py中的代码
class SongDetailView(DetailView):
model = Song
template_name = 'song/song_detail.html'
def get_context_data(self,**kwargs):
ctx = super().get_context_data(**kwargs)
if self.request.user.is_authenticated:
vote = Vote.objects.get_vote_or_unsaved_blank_vote(song=self.object, user = self.request.user)
if vote.id:
vote_url = reverse('music:song_vote_update', kwargs={'song_id':vote.song.id,'pk':vote.id}) #'pk':vote.id
else:
vote_url = reverse('music:song_vote_create', kwargs={'song_id':vote.song.id})
vote_form = SongVoteForm(instance=vote)
ctx['vote_form'] = vote_form
ctx['vote_url'] = vote_url
return ctx
class SongVoteCreateView(CreateView):
form_class = SongVoteForm
model = Vote
def get_success_url(self,**kwargs):
song_id = self.kwargs.get('song_id')
return reverse('music:song_detail', kwargs={'pk':song_id})
def form_valid(self, form):
user = self.request.user
song_obj = Song.objects.get(pk=self.kwargs['song_id'])
vote_obj, created = Vote.objects.get_or_create(song = song_obj, user = user)
form.instance = vote_obj
return super(SongVoteCreateView).form_valid(form)
class SongUpdateVoteView(UpdateView):
form_class = SongVoteForm
model = Vote
# def form_valid(self, form):
# user = self.request.user
# song_obj = Song.objects.get(pk=self.kwargs['song_id'])
# vote_obj, created = Vote.objects.get_or_create(song = song_obj, user = user)
# form.instance = vote_obj
# print(form)
# return super().form_valid(form)
def get_success_url(self):
song_id = self.kwargs.get('song_id')
return reverse('music:song_detail', kwargs={'pk':song_id})
网址映射
path('album/song/<int:pk>/',views.SongDetailView.as_view(), name='song_detail'),
path('album/song/create/<int:song_id>/',views.SongVoteCreateView.as_view(), name='song_vote_create'),
path('album/song/update/<int:song_id>/<int:pk>/', views.SongUpdateView.as_view(), name='song_vote_update')
html页面中的代码
<div>
<form action="{{vote_url}}" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ vote_form.as_p }}
<button class="btn btn-primary" type="submit" >Vote</button>
</form>
</div>
forms.py中的代码
class SongVoteForm(forms.ModelForm):
like = forms.ChoiceField(widget=forms.RadioSelect,choices=Vote.VALUE_CHOICE)
class Meta:
model = Vote
fields = ['like',]
响应进入控制台。
[31/Oct/2019 04:15:19] "GET /album/song/1/ HTTP/1.1" 200 3560
**[31/Oct/2019 04:15:21] "POST /album/song/update/1/8/ HTTP/1.1" 302 0**
[31/Oct/2019 04:15:21] "GET /album/song/1/ HTTP/1.1" 200 3560
答案 0 :(得分:0)
我已经在SongVoteCreateView中的form_valid中明确提到过,如下所示。现在,表单不再创建空白对象,而是重定向到正确的页面。
def form_valid(self, form):
like = form.cleaned_data.get('like')
user = self.request.user
song_obj = Song.objects.get(pk=self.kwargs['song_id'])
vote_obj, created = Vote.objects.get_or_create(song = song_obj, user = user, like=like)
form.instance = vote_obj
return super().form_valid(form)