Django : HTML form action directing to view (or url?) with 2 arguments

Yes i'm late but it can help others for better understanding how Django processes the request.

Django 3.0 pattern

How Django processes the request

Basic :

  1. First Django check the matching URL.
  2. If URL is matched then calling the defined view to process the request. (Success)
  3. If URL not matched/found the Django invokes error Page Not Found

In detail reading :

Official Django Documentations How Django processes a request


These are your URL patterns :

urlpatterns = [ path('profile/edit/<int:pk>/',views.editprofile, name='editprofile'),]

Third argument in urlpatterns is for if you want to change the url pattern from current to this :

urlpatterns = [ url('profile/edit/user/id/<int:pk>',views.editprofile, name = 'editprofile'),]

You don't need to redefine url pattern in all Templates where you using url name.

For Example :

This is my template profile.html where i used the url name instead of hard coded url.

<a class="item" href="{% url 'editprofile' user.id %}" >Edit profile </a>

Solution of your problem :

.html

Only use url name instead of hard coded url in your templates and pass arguments.

<form action={% process no_of_arguments  %}  method="POST">

views.py

Here you can process your request

def process(request,no_of_arguments):

Become good django developer

You can also use Django ModelForms for your model. Using model forms or simple form you can do multiple things

  • Modular approach
  • Write server side validation in related form instead of doing in views.py
  • Readable code - Clean code

You are trying to use the reverse resolution of urls in Django.

In your html file correct form action url to the following and method should be POST:

<form action={% url 'process' %}  method="POST">

In case you are trying to pass parameters along then use this:

<form action={% url 'process' request.user.id 4 %}  method="POST">

Reference: https://docs.djangoproject.com/en/1.10/topics/http/urls/