In views:
def article_add(request):
print request.user, " is adding an article"
if request.method == "POST":
web_url = request.POST['web_url']
Uploadarticle(web_url)
return redirect('myapp:index')
In html:
<form class="navbar-form navbar-right" role="form" method="post" action="{% url 'myapp:article_add' %}" enctype="multipart/form-data">
{% csrf_token %}
<div class="form-group">
<div class="col-sm-10">
<input id="article_url" name="web_url" type="text">
</div>
</div>
<button type="submit" class="btn btn-default"> + </button>
</form>
In url.py:
app_name = 'myapp'
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^$', views.article_add, name='article_add'),
]
What i'm trying to do here is to pass the url value through html to view, call the function to upload the database, redirect the user to the same home page as refresh then the newly added item will show up.
Somehow everytime I submit I got a blank page, in terminal I got an errors says:
Method Not Allowed (POST): /
"POST / HTTP/1.1" 405 0
As I can see in the code, you are using same URL for both view, so, whenever you hit URL /
, the request goes to first view(IndexView) which probably does not have any post method. Change the URL for article_add
view. Do like this:
app_name = 'myapp'
urlpatterns = [
url(r'^article-add/$', views.article_add, name='article_add'),
url(r'^$', views.IndexView.as_view(), name='index'),
]
You will be able to access view from URL {host_address}/article-add/
There is small mistake in your urls.py change your urls.py following way
app_name = 'myapp'
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^article-add/$', views.article_add, name='article_add'),
]
if you are incuded the 'myapp' urls.py in the main project urls.py then in the form in html just put action="{% url 'article_add' %}"
this way also.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With