Answer the question
In order to leave comments, you need to log in
How to make the button perform the actions that are written in the handler?
I want to add this anime to the manytomany field of the model when clicking on the button located on the anime page. I can't understand what I'm doing wrong because I'm doing it for the first time, maybe I didn't write the handler or the button or the form correctly, please help.
Model:
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
bio = models.TextField('Описание', max_length=500, blank=True)
avatar_image = models.ImageField('Аватар', upload_to='avatar/', blank=True)
shapka_image = models.ImageField('Шапка', upload_to='shapka/', blank=True)
watching = models.ManyToManyField(Anime, verbose_name='Смортю', blank=True, related_name='users_watching')
willwatch = models.ManyToManyField(Anime, verbose_name='Буду смотреть', blank=True, related_name='users_willwatch')
viewed = models.ManyToManyField(Anime, verbose_name='Просмотрено', blank=True, related_name='users_viewed')
thrown = models.ManyToManyField(Anime, verbose_name='Брошено', blank=True, related_name='users_thrown')
favorite = models.ManyToManyField(Anime, verbose_name='Любимые', blank=True, related_name='users_favoritepro')
class ProfileWatchingForm(forms.ModelForm):
class Meta:
model = Profile
fields = ('watching',)
<form action="." method="post">
{% csrf_token %}
<input type="submit" value="Watching anime" name="add_anime">
</form>
def watching_anime(request):
if request.POST:
if 'add_anime' in request.POST:
prof = Profile.objects.get(pk=request.user.id)
url = request.method['url']
anime = Anime.objects.get(url=url)
watch_form = ProfileWatchingForm(prof.watching.add(anime))
watch_form.save()
else:
watch_form = ProfileWatchingForm()
return render(request, 'anime/anime_detail.html', {'prof': prof, 'watch_form': watch_form})
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question