The question I'm looking to answer now is question 13
We've covered that a Django app is actually a project with a lot of apps inside it. (I should draw all these as flow diagrams, but unfortunately I don't have that option today...)
Some more important things about apps:
- If you use models, ie want to connect to a database, you must use apps in your project. For a simple webpage you don't need to create apps, but if you need database driven stuff in your site, you need to use models and thus apps.
13. What's the development process if I want to create a new app inside my website?
1. Create the App
Within your project folder /djangoproject create the new app by typing:
python manage.py startapp nameofapp
That creates a new a new app in your folder called "nameofapp", it creates the necessary files in a new folder inside your /djangoproject folder called "nameofapp" (depending on the name you give it that is).
Now under the /djangoproject/nameofapp you have the following files:
__init__.pySo Django automatically creates the files you need in the app.
models.py
tests.py
views.py
2. Create the Model
The defines the information that your app includes, for example, if your creating a blog, the model would define that there are posts which are textfields, dates for the posts, maybe different contributors and maybe also tags.
This is what the model might look like in the model.py file in /djangoproject/nameofapp:
from django.db import models
class Contibutor(models.Model):
name = models.CharField(max_length=10)
address = models.CharField(max_length=60)
website = models.URLField()
def __unicode__(self):
return self.name
class Meta:
ordering = ['name']
class Post(models.Model):
title = models.CharField(max_length=200)
body_html = models.TextField(blank=True)
pub_date = models.DateTimeField(auto_now_add=True)
contributors = models.ManyToManyField(Contributor)
You can find all the possible field types and information about those here:
http://docs.djangoproject.com/en/dev/ref/models/fields/#ref-models-fields
You can easily find information on what these classes and methods(?) mean, for example from the Django Book Chapter 5
After creating the model, you continue with creating the URLConfs, Views and Templates whicha have been discussed briefly in question 4. I'll be adding more information about that shrotly.
New question:
14. How do you cross query models? For example, If I have a blog app and a books app, how can I show related blog posts in my books app?
No comments:
Post a Comment