let us consider this..... ie.,
<form action="{% url 'contacts-new' %}"
so here we are using tags in the action attribute. that is ....
{% url 'contacts-new' %}
{%url%} is the template tags ok
then what is this contacts-new means where it is going......
let us see url.py........
url.py
=====
from django.conf.urls import patterns, include, url
from login.views import hello_page, home
from django.contrib import admin
admin.autodiscover()
import contacts.views
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'btms.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
# url(r'^admin/', include(admin.site.urls)),
url(r'^$', hello_page),
url(r'^home/', home),
url(r'^view/',contacts.views.ListContactView.as_view(),name='contacts-list',),
url(r'^new$', contacts.views.CreateContactView.as_view(), name='contacts-new',),
)
so in the url.py we added the
url(r'^new$', contacts.views.CreateContactView.as_view(), name='contacts-new',),
pattern : ^new$
include file : contacts.views.CreateContactView.as_view()
name : contacts-new
so in the include file let see what is happening ....... ie.,
contacts(app) => view.py => CreateContactView => as_view()(this is functionality to view)
View.py
========
from django.views.generic import ListView
from contacts.models import Contact
from django.core.urlresolvers import reverse
from django.views.generic import CreateView
class ListContactView(ListView):
model=Contact
template_name = 'contact_list.html'
class CreateContactView(CreateView):
model = Contact
template_name = 'edit_contact.html'
def get_success_url(self):
return reverse('contacts-list')
--------------------------- so that is fine
so let see more ie,
{% csrf_token %}
<ul>
{{ form.as_ul }}
</ul>
<input id='save_contact' type="submit" value="save"/>
</form>
so in these what is
{% csrf_token %} ?????????????
csrf=
After more investigation it appears the {% csrf_token %}
is
always inserted if the form has method post
and not if it doesn't.
Very clever auto protection from Django.
ie.,
<form action="{% url 'contacts-new' %}" method="post">
so better to use ....
------------------- next what we will see
<ul>
{{ form.as_ul }}
</ul>
let us know what is these actually doing .... ie.,
{{ form.as_ul }} ???????????
in the output.......
ie.,
i did not wrote the input box for every column ie., first name,, last name,, father name and email...
so when i given that in {{ form.as_ul}} i am getting that in unordered list becuase it is there in
between <ul> tag ....
so how it is happening.....
let us think what actually it is happens....
so .....
let us take files ie.,
edit_contact.html ,model.py , url.py and view.py
edit_contact.html
=================
<h1>add contact</h1>
<form action="{% url 'contacts-new' %}" method="post">
{% csrf_token %}
<ul>
{{ form.as_ul }}
</ul>
<input id='save_contact' type="submit" value="save"/>
</form>
<a href="{% url 'contacts-list' %}">back to list</a>
View.py ======== from django.views.generic import ListView
from contacts.models import Contact
from django.core.urlresolvers import reverse
from django.views.generic import CreateView
class ListContactView(ListView):
model=Contact
template_name = 'contact_list.html'
class CreateContactView(CreateView):
model = Contact
template_name = 'edit_contact.html'
def get_success_url(self):
return reverse('contacts-list')
in model file we have four column ie.,
model.py
=========
from django.db import models
class Contact(models.Model):
first_name = models.CharField(
max_length=255,
)
last_name = models.CharField(
max_length=255,
)
father_name = models.CharField(
max_length=255,
)
email = models.EmailField()
def __str__(self):
return ' '.join([
self.first_name,
self.last_name,
])
url.py
=======
from django.conf.urls import patterns, include, url
from login.views import hello_page, home
from django.contrib import admin
admin.autodiscover()
import contacts.views
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'btms.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
# url(r'^admin/', include(admin.site.urls)),
url(r'^$', hello_page),
url(r'^home/', home),
url(r'^view/',contacts.views.ListContactView.as_view(),name='contacts-list',),
url(r'^new$', contacts.views.CreateContactView.as_view(), name='contacts-new',),
)
flow steps
===========
-> so when i write {{form.as_url }}
-> it is searching from action url ie., contact-new
-> so it will check through url.py..for contact-new... ie.,
url(r'^new$', contacts.views.CreateContactView.as_view(), name='contacts-new',),
-> so it will go to contact----> view ---> CreateContactView ---->
and search for model name....... ie.,
model = Contact
->so in model.py ----> class Contact(models.Model): --> it will send the column name what ever it have...
-> so coming to this example it will have 4 column ie., first name,last name,father name and email...
-> that column are appear with respect data type for example .... i am writing .... email as something.... without proper pattern,,,,
so i am getting error becuase model is sending the datatype also..... so it will take default validation....... ....it is great na..
Advantage of {{form.as_ul}}
=================
it is avoiding the html work... and validation for basic that is the result ....all are saying it is easy to develop compare to other......
----------------------------------- next we will see
<a href="{% url 'contacts-list' %}">back to list</a>
this is anchor tag but in href see once again we use template tags......
so with this no need to write the complete path like
<a href="127.0.0.1:8080/view"> back to list</a>
if we change the ip address we need to change all......
so it will become more work.... to avoid these
django is using template tags..... great .....
No comments:
Post a Comment