so in the previous we added the information to the database like this.....
but this is for testing ok and for real time it not ok.........
interactive mode using shell script
===================================
(tutorial)$ python ./manage.py shell Python 2.7.3 (default, Aug 9 2012, 17:23:57) [GCC 4.7.1 20120720 (Red Hat 4.7.1-5)] on linux2 Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>> from contacts.models import Contact >>> Contact.objects.all() [] >>> Contact.objects.create(first_name='Nathan', last_name='Yergler') <Contact: Nathan Yergler> >>> Contact.objects.all() [<Contact: Nathan Yergler>] >>> nathan = Contact.objects.get(first_name='Nathan') >>> nathan <Contact: Nathan Yergler> >>> print nathan Nathan Yergler >>> nathan.id 1
so to avoid that we need to create new html page where we can add the feilds.....
so we will see that ........
so in the contact app i am changing the model.py file
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, ])
-------------
so in the above i added father name column
in the mysql
i drop the table ie.,
mysql> drop table contacts_contact; Query OK, 0 rows affected (0.04 sec) mysql> show tables; +----------------------------+ | Tables_in_mysite | +----------------------------+ | auth_group | | auth_group_permissions | | auth_permission | | auth_user | | auth_user_groups | | auth_user_user_permissions | | django_admin_log | | django_content_type | | django_session | +----------------------------+ 9 rows in set (0.00 sec)
in the terminal i am running this line ie.,
============================
ravi@ravi-Pegatron:~/btms$ python manage.py syncdb Creating tables ... Installing custom SQL ... Installing indexes ... Installed 0 object(s) from 0 fixture(s) ravi@ravi-Pegatron:~/btms$ python manage.py syncdb Creating tables ... Creating table contacts_contact Installing custom SQL ... Installing indexes ... Installed 0 object(s) from 0 fixture(s)
in mysql
=========
mysql> show tables; +----------------------------+ | Tables_in_mysite | +----------------------------+ | auth_group | | auth_group_permissions | | auth_permission | | auth_user | | auth_user_groups | | auth_user_user_permissions | | contacts_contact | | django_admin_log | | django_content_type | | django_session | +----------------------------+ 10 rows in set (0.00 sec) mysql> desc contacts_contact; +-------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | first_name | varchar(255) | NO | | NULL | | | last_name | varchar(255) | NO | | NULL | | | father_name | varchar(255) | NO | | NULL | | | email | varchar(75) | NO | | NULL | | +-------------+--------------+------+-----+---------+----------------+ 5 rows in set (0.00 sec)
so up to here i dropped and added the column
(but this is not good practice but for beginer it is fine)
create file in template of contacts app
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>
url.py (update)
=====
url(r'^new$', contacts.views.CreateContactView.as_view(), name='contacts-new',),
views.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 the url type
http://127.0.0.1:8000/new
after enter this check in the mysql also
mysql
mysql> select * from contacts_contact; +----+------------+-----------+----------------+----------------------+ | id | first_name | last_name | father_name | email | +----+------------+-----------+----------------+----------------------+ | 2 | deepak | kumar | muralidhar rao | just2deepu@gmail.com | +----+------------+-----------+----------------+----------------------+ 1 row in set (0.00 sec)
No comments:
Post a Comment