得到405错误,不能说为什么。网址似乎要检查POST方法...。我在这里缺少什么。
urls.py :
from django.conf.urls import url, include
from django.contrib import admin
from tweets.views import Index, Profile, PostTweet
admin.autodiscover()
urlpatterns = [
url(r'^$', Index.as_view()),
url(r'^user/(\w+)/$', Profile.as_view()),
url(r'^user/(\w+)/post/$', PostTweet.as_view()),
]
html :
{% extends "base.html" %}
{% block content %}
<div class="row clearfix">
<div class="col-md-12 column">
<form method = 'POST'>{% csrf_token %}
<!-- <div class="col-md-8 col-md-offset-2 fieldWrapper"> -->
{{ form.text.errors }}
{{ form.text }}
<!-- </div> -->
{{ form.country.as_hidden }}
<button type="submit" value="post it">Post
</form>
</div>
<!-- <div class="col-md-12 column">
{% for tweet in tweets %}
<div class="well">
<span>{{ tweet.text }}</span>
</div>
{% endfor %}
</div> -->
</div>
{% endblock %}
views.py :
class PostTweet(View):
def post(self, request):
if request.method =='POST':
print('post')
return HttpResponse('posted')
无论我做什么,仍然会遇到上述错误。 任何想法将不胜感激。 谢谢
答案 0 :(得分:0)
您需要在视图中更改发布方法的名称,它应该是post
而不是Post
。还需要更新方法签名,如下所示:
class PostTweet(View):
def post(self, request, *args, **kwargs): # renamed it post instead of Post
if request.method =='POST':
print(post)
return HttpResponse('posted')
也可以像这样更新表单:
<form method="post">{% csrf_token %}
<!-- <div class="col-md-8 col-md-offset-2 fieldWrapper"> -->
{{ form.text.errors }}
{{ form.text }}
<!-- </div> -->
{{ form.country.as_hidden }}
<button type="submit" value="post it">Post
</form>
您需要为视图提供一种get方法,以获取http get功能(我正在使用render()来显示html):
class PostTweet(View):
def post(self, request, *args, **kwargs): # renamed it post instead of Post
if request.method =='POST':
print(post)
return HttpResponse('posted')
def get(self, request, *args, **kwargs):
form = YourForm()
return render(request, 'your_html.html', {'form': form})
您还需要定义一个form(如果没有)以html形式呈现表单。
最后,我不知道您为什么要这么麻烦地呈现表单,您可以在其中简单地使用FormView来为您做任何事情。例如:
class YourForm(forms.Form):
country = forms.CharField()
class PostTweet(FormView):
form_class = YourForm
template_name = "path/to/template.html"
def form_valid(self, form):
print(form.cleaned_data) # it will show all the data submitted from the form
return super(YourFormView, self).form_valid(form)
如我所见,您的网址未使用正则表达式正确定义。您可以像这样更新它:
urlpatterns = [
url(r'^user/(?P<username>[-\w]+)/post/$', PostTweet.as_view()), # or any other variable
url(r'^user/(?P<username>[-\w]+)/$', Profile.as_view()),
url(r'^$', Index.as_view()),
]
因此,当您点击查看PostTweet
时,请使用/user/<any_username>/post/
,其中any_username
可以是您要使用的任何用户名。
答案 1 :(得分:0)
尝试一下:
{% extends "base.html" %}
{% block content %}
<div class="row clearfix">
<div class="col-md-12 column">
<form method = 'post'>{% csrf_token %} #<-- POST to post
<!-- <div class="col-md-8 col-md-offset-2 fieldWrapper"> -->
{{ form.text.errors }}
{{ form.text }}
<!-- </div> -->
{{ form.country.as_hidden }}
<button type="submit" value="post it">Post</button> #add this
</form>
</div>
<!-- <div class="col-md-12 column">
{% for tweet in tweets %}
<div class="well">
<span>{{ tweet.text }}</span>
</div>
{% endfor %}
</div> -->
</div>
{% endblock %}