9

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
viviwill
  • 1,235
  • 2
  • 13
  • 24
  • can you able to access the `article_add` views ? are you using funtion based views right ? can you please put your main urls.py here – Cadmus Feb 26 '17 at 05:17

2 Answers2

14

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/

ruddra
  • 40,233
  • 7
  • 54
  • 78
4

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.

Cadmus
  • 620
  • 7
  • 13