The questions I've had during my learning process.

Id Question Solved at
1 How do you install? Solved
2 How do you start developing? Solved
3 How do you make graphics with Django? Solved
4 Django uses some kind of structure to tell the application where to look for links. How does this hierarchy work? What is actually put into the different files? Solved
5 What is the most basic way to add the url:s to urls.py? Solved
6 What is the " '^ " thing that can be seen in the URLpatterns? Solved
7 What is the $ sign in the URLpatterns? Solved
8 If I want to embed an iframe to the app can I do it straight into the HTML? Pending
9 Are all of the URLpatterns always put into the urls.py file? Pending
10 Is it difficult to change to the production server? Pending
11 How do I really get an external CSS sheet working? Pending
12 How do I connect a new app that I've created under the project folder to the project? Pending
13 What's the development process if I want to create a new website with apps? Something discovered
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? Pending
15 How do I take the admin interface into use? Solved
16 How do I alter the models I already have? Pending
17 How do I customize what is shown and can be edited in the admin interface? Solved
18 When I want to start developing after some time, how do I start the server and check the page etc? Pending

Friday 29 May 2009

What's the development process if I want to create a new app inside my website?

After a bit of a pause the learning Django project continues. I've noticed this is quite a difficult way to follow the project, but that will be developed all the time. Now I'm planning on putting all the questions in the sidebar and then linking to the blogposts which include the answers. That should make it a bit easier to get your answers, and is a feature that will be included in the real learning process application.

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:
  1. 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.
But to the question (I've changed it a bit now because of the facts described above):

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__.py
models.py
tests.py
views.py

So Django automatically creates the files you need in the app.

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: