How can I print the url to the order id field of my django form?

Issue

I am doing a simple form site with Django. This is what my sites url is looks like: mysite.com/register/12345678
I want to print the part after the register (12345678) to the order id field. When someone goes this mysite.com/register/87654321 url then i want to print it.
How can i do that? These are my codes.(Currently using Django 1.11.10)

forms.py

from django import forms
from .models import Customer
from . import views

class CustomerForm(forms.ModelForm):

    class Meta:
        model = Customer
        fields = (
        'order_id','full_name','company','email',
        'phone_number','note')

        widgets = {
            'order_id': forms.TextInput(attrs={'class':'orderidcls'}),
            'full_name': forms.TextInput(attrs={'class':'fullnamecls'}),
            'company': forms.TextInput(attrs={'class':'companycls'}),
            'email': forms.TextInput(attrs={'class':'emailcls'}),
            'phone_number': forms.TextInput(attrs={'class':'pncls'}),
            'note': forms.Textarea(attrs={'class':'notecls'}),

        }

views.py

from django.shortcuts import render
from olvapp.models import Customer
from olvapp.forms import CustomerForm
from django.views.generic import CreateView,TemplateView


def guaform(request,pk):

    form = CustomerForm()

    if request.method == "POST":
        form = CustomerForm(request.POST)

        if form.is_valid():
            form.save(commit=True)

        else:
            print('ERROR FORM INVALID')

    theurl = request.get_full_path()
    orderid = theurl[10:]

    return render(request,'forms.py',{'form':form,'orderid':orderid})

customer_form.html

{% extends 'base.html' %}

{% block content %}

<h1>REGÄ°STRATÄ°ON</h1>

<form class="registclass" method="POST">
  {% csrf_token %}
  {{ form.as_p }}

  <button type="submit" class="btn btn-default">REGISTER</button>

</form>

{% endblock %}

urls.py

from django.conf.urls import url
from django.contrib import admin
from olvapp import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^thanks/$',views.ThankView.as_view(),name='thank'),
    url(r'^register/(?P<pk>\d+)',views.guaform,name='custform'),

]

Solution

SamSparx is right, here’s some additional information to help prevent such errors in advance:

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^thanks/$',views.ThankView.as_view(),name='thank'),
    url(r'^register/(?P<pk>\d+)',views.guaform,name='custform'),
]

You are using regex to parse your path. Regex is generally speaking not recommended in this case according to the docs because it might introduce more errors and is harder to debug. (Unoptimized) RegEx also tends to be slower.

For simple use cases such as your path here, consider choosing the default pathing syntax as below:

urlpatterns = [
    url('admin/', admin.site.urls),
    url('thanks/',views.ThankView.as_view(),name='thank'),
    url('register/<int:pk>',views.guaform,name='custform'),
]

You could of course also use string instead of int depending on your usage of pk.

Your paths do not all end with a slash consistently. This might impact your SEO and confuse users. See this and this.

Also your form is not imported .as_view() for some reason which could cause some problems.

Answered By – Shellsort

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published