Home > Software engineering >  How can I print the url to the order id field of my django form?
How can I print the url to the order id field of my django form?

Time:05-16

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  method="POST">
  {% csrf_token %}
  {{ form.as_p }}

  <button type="submit" >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'),

]

CodePudding user response:

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.

CodePudding user response:

You have passed the value to your view as 'pk' so you can use that to set the the initial value:

views.py

form = CustomerForm(initial={'order_id': pk})
  • Related