Django – Series – Post – 13 – Saving data to DB – Function based view

Django – Series – Post – 13 – Saving data to DB – Functions based view
Spread the love

 

 

Django – Series – Post – 13 – ​​ Saving data to DB – Functions based view :

In this blog post , we will see how to save the data obtained from a form using a function based view.​​ 

First create “forms.html” under the ‘templates/restaurants/’ folder , In this html file we will extend the base.html file and create a form with 3 input fields and a save button. We will also use bootstrap to beautify the contents.​​ 

We will make the form method as post and for security we will use the {% csrf_token %}

 

{% extends 'base.html' %}
{% block title %}Add Restaurant{% endblock %}

{% block page %}
<h3>Add restaurants</h3><br>
<div​​ class=​​ "col-sm-6">
 ​​ ​​ ​​​​ <form​​ METHOD="POST">​​ {% csrf_token %}
 ​​ ​​ ​​ ​​ ​​​​ <div​​ class="form-group form-inline ">
 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ <label​​ class="col-sm-2 col-form-label">Name</label>
 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ <input​​ type="text"​​ class="form-control"​​ name="title" ​​ placeholder="Enter restaurant name">
 ​​ ​​ ​​ ​​ ​​​​ </div>
 ​​ ​​ ​​ ​​ ​​​​ <div​​ class="form-group form-inline​​ ">
 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ <label​​ class="col-sm-2 col-form-label">Location</label>
 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ <input​​ type="text"​​ class="form-control" ​​ name=​​ "location" ​​ placeholder="Enter restaurant location">
 ​​ ​​ ​​ ​​ ​​​​ </div>
 ​​ ​​ ​​ ​​ ​​​​ <div​​ class="form-group form-inline ">
 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ <label​​ class="col-sm-2 col-form-label">Category</label>
 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ <input​​ type="text"​​ class="form-control"​​ name=​​ "category" ​​ placeholder="Enter restaurant Category">
 ​​ ​​ ​​ ​​ ​​​​ </div>
 ​​ ​​ ​​ ​​ ​​​​ <button​​ type="submit"​​ class="btn btn-primary col-sm-2">Save</button>
 ​​ ​​ ​​​​ </form>

</div>
{% endblock %}

 

Now we will create a view for this html file, create a function restaurant_createView in “views.py” which will handle the request, if the request method is post, it will get the values from the form and pass these values to the DB object​​ for creation, we will also use HttpResponseRedirect to redirect the url back to restaurants list post creation.​​ 

from​​ django.db.models​​ import​​ Q
from​​ django.views​​ import​​ View
from​​ django.shortcuts​​ import​​ render, get_object_or_404
from​​ django.views.generic​​ import​​ TemplateView, ListView, DetailView
from​​ django.http​​ import​​ HttpResponse, HttpResponseRedirect
from​​ .models​​ import​​ restaurantLocations
from​​ .forms​​ import​​ RestaurantFormCreation

def​​ restaurant_createView(request):
 ​​ ​​ ​​​​ if​​ request.method =='POST'​​ :
 ​​ ​​ ​​​​  ​​ ​​ ​​​​ title = request.POST.get("title")
 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ location = request.POST.get("location")
 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ category = request.POST.get("category")
 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ obj​​ = restaurantLocations.objects.create(
 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ name​​ = title,
 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ location​​ = location,
 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ category​​ = category
 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ )
 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ return​​ HttpResponseRedirect('/restaurants/')
 ​​ ​​ ​​​​ template_name =​​ 'restaurants/forms.html'
 ​​ ​​ ​​​​ context={}
 ​​ ​​ ​​​​ return​​ render(request,template_name,context)

 

Now we need to make the changes in “urls.py”, Here will add the​​ function based view and we will define the url pattern​​ r'^restaurants/create/$’​​ for this view.​​ 

 

from​​ django.conf.urls​​ import​​ url
from​​ django.contrib​​ import​​ admin
from​​ django.views.generic​​ import​​ TemplateView
from​​ restaurants.views​​ import​​ (
 ​​ ​​ ​​​​ restaurant_listView,
 ​​ ​​ ​​​​ searchandlistRestaurants,
 ​​ ​​ ​​​​ restaurantDetailview,
 ​​ ​​ ​​​​ restaurant_createView,
)

urlpatterns = [
 ​​ ​​ ​​​​ url(r'^admin/', admin.site.urls),
 ​​ ​​ ​​​​ url(r'^$', TemplateView.as_view(template_name="home.html")),
 ​​ ​​ ​​​​ url(r'^restaurants/$', restaurant_listView),
 ​​ ​​ ​​​​ url(r'^restaurants/create/$', restaurant_createView),  ​​ ​​​​ 
 ​​ ​​ ​​​​ url(r'^restaurants/(?P<slug>[\w-]+)/$', restaurantDetailview.as_view()),
 ​​ ​​ ​​​​ url(r'^works/$', TemplateView.as_view(template_name="works.html")),
 ​​ ​​ ​​​​ url(r'^cities/$', TemplateView.as_view(template_name="cities.html")),
 ​​ ​​ ​​​​ url(r'^plans/$', TemplateView.as_view(template_name="plans.html")),
]

 

 

Now we need to adjust the slug generator under “utils.py”, for ​​ /create/, this is because if we add a restaurant with the name “create”, it will create a slug name as create , but we already have a url pattern for /create/, so we need to by pass it with the below logic.​​ 

 

import​​ random
import​​ string
from​​ django.utils.text​​ import​​ slugify


def​​ random_string_generator(size=10,​​ 

chars=string.ascii_lowercase + string.digits):
 ​​ ​​ ​​​​ return​​ ''.join(random.choice(chars)​​ for​​ _​​ in​​ range(size))

DONT_USE=['create']
def​​ unique_slug_generator(instance, new_slug=None):
 ​​ ​​ ​​​​ """
 ​​ ​​ ​​​​ This is for a Django project and it assumes your instance
 ​​ ​​ ​​​​ has a model with a slug field and a title character (char) field.
 ​​ ​​ ​​​​ """
 ​​ ​​ ​​​​ if​​ new_slug​​ is not None:
 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ slug = new_slug
 ​​ ​​ ​​​​ else:
 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ slug = slugify(instance.title)
 ​​ ​​ ​​​​ if​​ new_slug​​ in​​ DONT_USE:
 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ new_slug =​​ "{slug}-{randstr}".format(
 ​​ ​​ ​​ ​​​​  ​​ ​​ ​​ ​​ ​​ ​​​​ slug=slug,
 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ randstr=random_string_generator(size=4)
 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ )
 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ return​​ unique_slug_generator(instance,​​ new_slug=new_slug)

 ​​ ​​ ​​​​ Klass = instance.__class__
 ​​ ​​ ​​​​ qs_exists = Klass.objects.filter(slug=slug).exists()
 ​​ ​​ ​​​​ if​​ qs_exists:
 ​​ ​​​​  ​​ ​​ ​​ ​​​​ new_slug =​​ "{slug}-{randstr}".format(
 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ slug=slug,
 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ randstr=random_string_generator(size=4)
 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ )
 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ print(new_slug)
 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ return​​ unique_slug_generator(instance,​​ new_slug=new_slug)
 ​​ ​​ ​​​​ return​​ slug

 

Now lets open up the url to see if this works as expected,​​ 

 

Thanks for viewing the post, please post your comments below.

Please Like & Follow us on Facebook & Twitter,

https://www.facebook.com/gadoth/

https://www.twitter.com/GadothDA/

The code is available in the below github link,

https://github.com/rajathithan/Django

 

 

 

Related posts

Leave a Comment