11

Started learning django about a week ago and ran into a wall. Would really appreciate any enlightenment...

models.py

class data(models.Model):
    course = models.CharField(max_length = 250)

    def __str__(self):
        return self.course

html

Converted the objects in models.course to schlist

<link rel="stylesheet" type="text/css" href="{% static '/chosen/chosen.css' %}" />
<form action={% views.process %}  method="GET">
      <div>
        <h4 style="font-family:verdana;">First Course: </h4>
        <select data-placeholder="Course" style="width:350px;" class="chosen-select" tabindex="7">
          <option value=""></option>
          {% for item in schlist %}
          <option> {{ item }} </option>
          {% endfor %}
        </select>
      </div>
      </br>
      <div>
        <h4 style="font-family:verdana;">Second Course:</h4>
        <select data-placeholder="Course" style="width:350px;" class="chosen-select" tabindex="7">
          <option value=""></option>
          {% for item in schlist %}
          <option> {{ item }} </option>
          {% endfor %}
        </select>
      </div>
      </br>
  <input type="submit" value="Compare!" />
</form>

urls.py (having my doubts if this works..)

urlpatterns = [
    url(r'^(\d+)/(\d+)$',views.process, name = 'process'),
]

view.py

def process(request,q1 ,q2):
    obj1= get_object_or_404(Schdata, course = q1)
    obj2= get_object_or_404(Schdata, course = q2)
 ........

Was wondering if it is possible for the form action to direct the action to

(1) view.py or (2) url.py (and eventually to a view.py) with 2 arguments selected?

If so how should the form action be? {{view ?}} or {{url ?}}. Am I missing out the definition of my arguments in my HTML?

Directing to views.py:

User input is CharField, could use get_object_or_404 to get the model pk. However when defining my urls.py I would get a Noreverse error as my url arguments is the primary key.

Directing to urls.py:

Url arguments is primary key. From the way I see it, I need to magically convert my User input Charfield to a pk before passing it to urls.py

Is there a (or) function for get() in django? E.g get_object_or_404(pk = q1 or course = q1)?

Would really appreciate any advice. Been staring at this for hours.

Marius Mucenicu
  • 1,685
  • 2
  • 16
  • 25
Lolerla
  • 131
  • 1
  • 1
  • 8

2 Answers2

32

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/

Abijith Mg
  • 2,647
  • 21
  • 35
13

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