Я пытаюсь прикрепить форму информационного бюллетеня к моей странице postdetail (HitcountDetailView), я попробовал Formmixin, и моя регистрация информационного бюллетеня работала правильно, но перенаправление URL-адреса не работает.
* 1002 аргументы ключевого слова '{' id ':,' slug ':' jobs-available-96 '}' не найдены. Пробовали 1 шаблон (ы): ['blog \ / details \ / (? P [-a-zA-Z0-9 _] +) \ / (? P [0-9] +) \ / $']
views.py
class Blog_Detail(FormMixin,HitCountDetailView):
model=BlogPost
template_name = 'blog/blogDetail.html'
context_object_name = 'posts'
slug_field = 'slug'
count_hit = True
id=id
form_class=NewsletterForm
def get_success_url(self,id=None):
if 'slug' in self.kwargs:
slug = self.kwargs['slug']
else:
slug = 'demo'
return reverse('details', kwargs={"id":self.id,'slug': slug})
def get_context_data(self, **kwargs):
instance = super(Blog_Detail, self).get_context_data(**kwargs)
instance['form'] = NewsletterForm()
instance.update({
'popular_posts': BlogPost.objects.order_by('-hit_count_generic__hits')[:3],
'recent_posts': BlogPost.objects.filter().order_by('-updated')[:3],
})
return instance
def post(self,request, *args, **kwargs):
form=NewsletterForm(request.POST or None)
if form.is_valid():
instanceMail=form.save(commit=False)
if NewsletterUser.objects.filter(email=instanceMail.email).exists():
messages.warning(request,"Your Email is aldready exists")
self.get_success_url()
else:
instanceMail.save()
messages.success(request,"Your Email" +"\t" +instanceMail.email ))
subject="Thank you for Joining in JOBvsJOB"
from_email=settings.EMAIL_HOST_USER
to_email=[instanceMail.email]
d={}
html_templatey=get_template("blog/UserSubscribeMail.html").render(d)
welcomemessage="Welcome"
email=EmailMultiAlternatives(subject,'',from_email,to_email)
email.attach_alternative(html_templatey,'text/html')
email.send()
self.get_success_url()
form=NewsletterForm()
args={"form":form}
return render(request,'blog/blogDetail.html',args)
urls.py
urlpatterns=[
path('details/<slug:slug>/<int:id>/',views.Blog_Detail.as_view(),name="details"),
]
slugfield и обратные методы в models.py
def get_absolute_url(self):
return reverse('details',kwargs={"id":self.id,"slug":self.slug})
def save(self,*args,**kwargs):
self.slug=slugify(self.postTitle) + "-" + str(self.id)
super().save(*args,**kwargs)