How to write dynamic Django URL's to capture data and pass to a view for processing

So I am writing a simple website with Django, with some basic CRUD features. The website has 7 main models in total.

  • Contact Details

  • Personal Details

  • Mobile Provider Details

  • TV Provider Details

  • Broadband Provider Details

  • Internet Provider Details

  • User Consent

The way I have wrote the website, I have created a URL path for each model, each with a Create, Update and Delete action (see below snippet)

    path('addcontactdetails', views.addContactDetail, name='addcontactdetails'),
    path('updatecontactdetails', views.updateContactDetail, name='updatecontactdetails'),
    path('deletecontactdetails', views.deleteContactDetail, name='deletecontactdetails'),

For each URL path, I have then created corresponding views to process the requests (see below snippet from addContactDetail view).

@login_required(login_url='login')
def addContactDetail(request):
    data_to_capture = 'Contact Details'
    form = ContactDetailForm()

    if request.method == 'POST':
        form = ContactDetailForm(request.POST)
        current_user = request.user

        ContactDetail.objects.create(
            user = current_user,
            user_email = request.POST.get('user_email'),
            mobile_phone_no = request.POST.get('mobile_phone_no'),
            door_no_or_house_name = request.POST.get('door_no_or_house_name'),
            address_line_1 = request.POST.get('address_line_1'),
            address_line_2 = request.POST.get('address_line_2'),
            town_or_city = request.POST.get('town_or_city'),
            post_code = request.POST.get('post_code'),
        )
        return redirect('profile')

    context = {
        'data_to_capture': data_to_capture,
        'form': form
    }
    return render(request, 'base/capture_data.html', context)

I am wondering if there is a more sophisticated way to write my URL paths, so that I can reduce the number of URL paths I need, and limit the number of views I need. I would have though there would be a way to capture the data from the URL path, and then dynamically pass the appropriate data to the HTML template, but I am not sure how to achieve this.

Thanks for taking the time to read my question, and appreciate any help or advice which can be offered! :)

I tried going back to the bare bones, and tried to capture data from the URL path, and pass it to the view, to then pass data onto the HTML template, however was struggling to make headway.

Back to Top