0

models.py

class Author(models.Model):
    author_id = models.AutoField(primary_key=True)
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=40)
    email = models.EmailField()
    age=models.IntegerField()

    class Meta:
    db_table=u'Author'

    def __unicode__(self):
        return u"%d %s %s %s %d" % (self.pk, self.first_name, self.last_name, self.email,self.age)



class Book(models.Model):
    book_id=models.AutoField(primary_key=True,unique=True)
    book_name=models.CharField(max_length=30)
    publisher_name=models.CharField(max_length=40)
    author=models.ForeignKey(Author)

    class Meta:
        db_table = u'Book'

    def __unicode__(self):
        return u'%d %s %s' % (self.pk, self.book_name, self.publisher_name)

can you please tell me how to write a forms.py and views.py for this model to display the data from database.

my forms.py is

class AuthorForm(forms.ModelForm):

    class Meta:
        model = Author
        fields = ['author_id','first_name','last_name','email','age']

class BookForm(forms.ModelForm):

    class Meta:
        model = Book
        fields=['book_id','book_name','publisher_name','author_id']

So i writen the forms.py in this way it is writen for models field.So please tell me that the form what i given is correct,if not please tell me how to write a forms.py and views.py for displaying the data from database from two tables.

my views.py is

def index(request): 
    book = forms.ModelMultipleChoiceField(queryset=Book.objects.all())
    return render_to_response('index.html', locals(),context_instance=RequestContext(request))

I am not able to show all the data from database.please help me with this.If any problem with my views.py content guide me how to write views.py

my index.html is

<html>
<head>
<title>
</title>
</head>
<body>   
<div align="center">
<table border="0" cellpadding='8' cellspacing='10'>
    <tr>
        <td align="right" colspan="10"><a href="/addbook/">Add Book</a></td>
    </tr>
    <tr>
        <th>Book Id</>
    <th>Book name</th>
    <th>Publication name</th>
    <th>Author Id</th>
    <th>First Name</th>
    <th>Last Name</th>
    <th>E Mail</th>
    <th>Age</th>
        <th></th>
        <th></th>
    </tr>
    {% for book in books %}
    <tr>
       <td>{{ book.book_id }}</td>
       <td>{{ book.book_name }}</td>
       <td>{{ book.publisher_name }}</td>
       <td>{{ book.author_id }}</td>    
       <td>{{ book.author.first_name }}</td>
       <td>{{ book.author.last_name }}</td>
       <td>{{ book.author.email }}</td>
       <td>{{ book.author.age }}</td>
       <td><a href="/editbook/{{ book.book_id}}">Edit</a></td>
       <td><a href="/deletebook/{{ book.book_id}}">Delete</a></td>
    {% endfor %}
</table>
</div>
</body>
</html>

Thanks,

6
  • do you want to show all the data in your models? Commented Mar 6, 2013 at 4:45
  • So, if you are not trying to edit some date or add some things to your models you don't need to use forms. You can create a view to do this or use a generic view. Try this: docs.djangoproject.com/en/1.2/ref/generic-views/… Commented Mar 6, 2013 at 4:50
  • ya but i am trying this using forms.py sir,please tel me how to move Commented Mar 6, 2013 at 4:55
  • can any one please tell me how to write views.py for the above snippets.Please see the views.py which i writen is correct Commented Mar 6, 2013 at 5:37
  • how is your index.html? Commented Mar 6, 2013 at 5:40

2 Answers 2

1

try this in your views.py:

from models import Book
from django.shortcuts import render_to_response
from django.template import RequestContext

def index(request): 
   books = Book.objects.all()
   return render_to_response('index.html', 
       locals(),
       context_instance=RequestContext(request))
Sign up to request clarification or add additional context in comments.

2 Comments

yes sir,it is working.here we are not using forms to display the answer.Is it possible to do the same using forms.py if possible please tell me how to do..Thanks
actually, when you create a form in django is to create or edit some data. To show your models you may use views.py, ok? Good luck!
1

Your models and forms look correct off hand. Please see part 4 of the Django tutorial for help writing views to connect them.

3 Comments

can you please tell me how to write a views.py to display the data from database
ya but i am trying this using forms.py sir,please tel me how to move
If you're using the UpdateView generic view, but want to have a custom form, you can specify the form_class on the view. See this example: gist.github.com/flaviamissi/1275204

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.