About me

Tuesday, May 15, 2018

Python django chap 6 (CREATING VIEWS)

Friends this is the continuation from its previous post so i kindly recommend you to read that post i have made this very simple even a complete beginner can learn it and if you are beginner in python then you need to learn python i have made that blog also for that click this link


CREATING VIEWS 

                 The views in django is simple python function that takes a web request and give a web response which can be a html content of the web page or a 404 error, or an XML document, or an image, etc. it is a type of web page that serves a specific function and has a specific template like in this blog it has different views like 

  •   Blog homepage – displays the latest few entries. 

  •   Entry “detail” page – permalink page for a single entry. 

  •   Year-based archive page – displays all months with entries in the given year.

  •   Month-based archive page – displays all days with entries in the given month.

  •   Day-based archive page – displays all entries in the given day. 

  •   Comment action – handles posting comments to a given entry.

Django will choose a view by examining the URL that’s requested (to be precise, the part of the URL after the domain name). You should remember that  To get from a URL to a view, Django uses what are known as ‘URLconfs’. A URLconf maps URL patterns to views in our myproj/views.py. to get more info on this you can go to URL dispatcher
Now we will create a simple view

 from django.http import HttpResponse

def hello(request):
   text = """<h1>welcome to my app !</h1>"""
   return HttpResponse(text)</div>
In here we have used the httpresponse to render the html code. To see this view as a page we need to map it to the url

Now to map it we will do as our myproj/url.py add this code


from django.conf.urls import patterns, include, url
 urlpatterns = patterns('',
 url(r'^hello/', 'myapp.views.hello', name = 'hello'),
 
 )

The marked line maps the URL "/home" to the hello view created in myapp/view.py file.mapping is composed of three elements
  • The pattern − A regexp matching the URL you want to be resolved and map. Everything that can work with the python 're' module is eligible for the pattern (useful when you want to pass parameters via url).
  • The python path to the view − Same as when you are importing a module
  • The name − In order to perform URL reversing, you’ll need to use named URL patterns as done in the examples above. Once done, just start the server to access your view via :http://127.0.0.1/hello
Each views is responsible for doing one of two things like returning the httpresponse object containing the content for the requested page, or raising an exception such as Http404. The rest is up to us.Your view can read records from a database, or not. It can use a template system such as Django’s – or a third-party Python template system – or not. It can generate a PDF file, output XML, create a ZIP file on the fly, anything you want, using whatever Python libraries you want.

TEMPLATES


 Template convenient way to generate HTML dynamically A template contains the static parts of the desired HTML output as well as some special syntax describing how dynamic content will be inserted..Django uses templates to make it possible to separate python and HTML, the python goes in views and HTML goes in templates. To link the two, Django relies on the render function and the Django Template language.Django ships built-in backends for its own template system, creatively called the Django template language (DTL)  The page’s design is hard-coded in the view. If you want to change the way the page looks, you’ll have to edit this Python code. So let’s use Django’s template system to separate the design from Python by creating a template that the view can use. for that we need to create a directory called template in our myproj directory Your project’s TEMPLATES setting describes how Django will load and render templates. The default settings file configures a DjangoTemplates backend whose APP_DIRS option is set to True. By convention DjangoTemplates looks for a “templates” subdirectory in each of the INSTALLED_APPS. Within the templates directory you have just created, create another directory called myproj, and within that create a file called index.html. In other words, your template should be at myproj/templates/myproj/index.html. Because of how the app_directories template loader works as described above, you can refer to this template within Django simply as myproj/index.html.
more on templates will be include in the next post
THANKS FOR VIEWING MY BLOG HOPE YOU LIKE THE POST IF YOU WANT TO GET LATEST POSTS ON THIS PLEASE FOLLOW BY EMAIL ID AND FEEL FREE TO ASK YOUR DOUBTS AND DON'T FORGET TO SHARE,COMMENT AND FOLLOW 

1 comment: