需要帮助解决基本模板中的时事通讯表单问题
Need help to solve newsletter form issues in base template
这是我在 base.html 中的内容(在页脚内,因此此新闻通讯表格将出现在每一页中)
<form action="" method="POST">
{% csrf_token %}
<div class="form-group">
<div class="input-group mb-3">
<input type="text" class="form-control" placeholder='Enter email address' onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter email address'">
<div class="input-group-append">
<button class="btn" type="submit"><span class="lnr lnr-arrow-right"></span></button>
</div>
</div>
</div>
</form>
这是模型 (subscribe/models.py)
class Subscriber(models.Model):
email = models.EmailField()
timestamp = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.email
我有什么 views.py
def subscribe_form(request):
if request.method == 'POST':
email = request.POST.get('email')
new_email = Subscriber()
new_email.email = email
new_email.save()
return redirect('home-page')
这里是urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.PostListView.as_view(), name='home-page'),
path('subscribe/', views.subscribe_form, name='subscriber'),
path('archive/', views.archive, name='archive-page'),
path('category/', views.category, name='category-page'),
path('contact/', views.contact, name='contact-page')
]
提交提交按钮后,我在 shell
中收到此错误
Method Not Allowed (POST): /
Method Not Allowed: /
[18/Jan/2020 04:13:11] "POST / HTTP/1.1" 405 0
所以,我是一个初学者,我正在尝试建立一个博客,但我没有找到任何有用的解决方案来解决这个问题。也许我完全错了,但无论如何,如果有人能帮助我完成这项工作。
谢谢大家
在您的索引 URL 中,您不能 post.So 将其更改为订阅/
<form action="{% url 'subscriber' %}" method="POST>
这是我在 base.html 中的内容(在页脚内,因此此新闻通讯表格将出现在每一页中)
<form action="" method="POST">
{% csrf_token %}
<div class="form-group">
<div class="input-group mb-3">
<input type="text" class="form-control" placeholder='Enter email address' onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter email address'">
<div class="input-group-append">
<button class="btn" type="submit"><span class="lnr lnr-arrow-right"></span></button>
</div>
</div>
</div>
</form>
这是模型 (subscribe/models.py)
class Subscriber(models.Model):
email = models.EmailField()
timestamp = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.email
我有什么 views.py
def subscribe_form(request):
if request.method == 'POST':
email = request.POST.get('email')
new_email = Subscriber()
new_email.email = email
new_email.save()
return redirect('home-page')
这里是urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.PostListView.as_view(), name='home-page'),
path('subscribe/', views.subscribe_form, name='subscriber'),
path('archive/', views.archive, name='archive-page'),
path('category/', views.category, name='category-page'),
path('contact/', views.contact, name='contact-page')
]
提交提交按钮后,我在 shell
中收到此错误Method Not Allowed (POST): /
Method Not Allowed: /
[18/Jan/2020 04:13:11] "POST / HTTP/1.1" 405 0
所以,我是一个初学者,我正在尝试建立一个博客,但我没有找到任何有用的解决方案来解决这个问题。也许我完全错了,但无论如何,如果有人能帮助我完成这项工作。 谢谢大家
在您的索引 URL 中,您不能 post.So 将其更改为订阅/
<form action="{% url 'subscriber' %}" method="POST>