我正在不允许此方法(发布),就像我无法发布表格一样,我可能做错了什么。我是django的新手。我的url.py可能是错误的,还是我的form-id不正确,导致无法发布数据。我在这里有form.py,不知道这是否可能是我的问题
views.py
class ContactUsView(ContactFormView):
""" Contact Us Form View """
form_class = ContactUsForm
def get_success_url(self):
return reverse('messaging:contact_form_sent')
def clear_notification(request):
""" Clears the notification for a given user"""
Notifications.objects.filter(user=request.user).delete()
return redirect(reverse('accounts:dashboard', args=(request.user,)))
urls.py
urlpatterns = [
path(
'contact/',
views.ContactUsView.as_view(),
name='contact_form'),
path(
r'contact/sent/',
TemplateView.as_view(
template_name='contact_form/contact_form_sent.html'),
name='contact_form_sent'),
path(r'clear/', views.clear_notification, name='clear_notification'),
]
contact_form.html
<div class="row">
<form id='ContactUsForm' method='post' action={% url 'messaging:contact_form_sent' %}><br>
{% csrf_token %}
<div class="col-md-6">
<div class="form-group">
{% csrf_token %}
<input type="text" class="form-control input-upper" id="fullname" placeholder="John Doe" name="fullname">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
{% csrf_token %}
<input type="email" class="form-control input-upper" id="email" placeholder="Email" name="email">
</div>
</div>
</div><br><br>
<div class="row">
<div class="col-md-6">
<div class="form-group">
{% csrf_token %}
<input type="text" class="form-control input-upper" id="subject" placeholder="Subject" name="subject">
</div>
</div>
</div><br>
<div class="row">
<div class="col-md-10">
<div class="form-group">
{% csrf_token %}
<textarea class="form-control input-upper" id="message" placeholder="Message" name="message"></textarea>
</div>
{% csrf_token %}
<button type="submit" class="btn btn-primary btn-send-contact" value="SUBMIT">SEND</button>
</div>
</div><br><br>
</form>
</div>
</div>
</div>
{% endblock %}
基本视图
"""
"""
from django.views.generic.edit import FormView
from .forms import ContactForm
try:
from django.urls import reverse
except ImportError: # pragma: no cover
from django.core.urlresolvers import reverse # pragma: no cover
class ContactFormView(FormView):
form_class = ContactForm
recipient_list = None
template_name = 'contact_form/contact_form.html'
def form_valid(self, form):
form.save()
return super(ContactFormView, self).form_valid(form)
def get_form_kwargs(self):
# ContactForm instances require instantiation with an
# HttpRequest.
kwargs = super(ContactFormView, self).get_form_kwargs()
kwargs.update({'request': self.request})
# We may also have been given a recipient list when
# instantiated.
if self.recipient_list is not None:
kwargs.update({'recipient_list': self.recipient_list})
return kwargs
def get_success_url(self):
return reverse('contact_form_sent')
答案 0 :(得分:0)
默认情况下,TemplateView
不允许POST
,您需要做的就是覆盖post方法。
示例:
class ContactUsView(TemplateView):
template_name = 'contact_form/contact_form_sent.html'
def post(self, request, *args, **kwargs):
context = self.get_context_data()
if context["form"].is_valid():
print 'POST IS HERE'
return super(TemplateView, self).render_to_response(context)
def get_context_data(self, **kwargs):
context = super(ContactUsView, self).get_context_data(**kwargs)
form = ContactUsEmailForm(self.request.POST or None)
context["form"] = form
return context
答案 1 :(得分:0)
您的表单将直接发布到不希望发布的确认页面contact_form_sent。通过这样做,它绕过了实际上正在处理帖子ContactUsView的视图。
您的表单操作应针对library(tidyverse)
df %>%
complete(car = unique(car), year = 2012:2014, month = 1:12,
nesting(country), fill = list(amount = 0))
。