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
Showing posts with label django. Show all posts
Showing posts with label django. Show all posts

Friday, 15 May 2009

The Django Structure - from urlconf to view, to model...


This is a continuation of the Capturing Learning Django project.

Just some more explanation for question nr. 4... I'm coming back to these questions since I want to document the process of learning. So even though I'm going back to some of the questions, capturing the progress is also important... However, I'll only post the new stuff from now on.


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?


Got some more answers to this issue. I'll now try to explain what happens in my app. A more illustrative picture is attached below.

I have a blog, under /djangoblog/blog. So I input http://127.0.0.1:8000/blog in my browser. Then:
  1. A URL call matches a URL in the urls.py file which is in /djangoblog/urls.py
  2. In the urls.py file, I have this line: (r'^blog/', include('djangoblog.blog.urls')), which redirects to /djangoblog/blog.urls.py
  3. In there, one of the lines is
    • (r'^$','archive_index', dict(info_dict, template_name='blog/list.html')),
    • This line fits the url pattern ...8000/blog ... because of ^$ which matches an empty string after the /blog.
    • This line redirects to blog/list.html
  4. Then my template blog/list.html takes over. It resides in djangoblog/templates/blog/list.html. I think Django can find it automatically from there since I've configured the templates in the settings.py-file.
    • This file starts with {% extends 'base.html' %}, and thus first loads my base.html template, which is found in djangoblog/templates/base.html
    • This list.html file then says to the browser what is shown. It's an html-file! The special thing is is that it's also full of Django/python.
  5. The Django/Python calls, such as:

    {{ object.pub_date|date:"D d M Y" }}

    , then call up the methods from the djangoblog/blog/models.py-file.
  6. That's it. The model is the actual Python code that says what the page shows.

It seems the explanation above is missing views. So with views, the urlconf actually calls a function in the views.py file:

1. In my urlconf, I have for example:

(r'^polls/(?P<poll_id\d+)/$, 'learness2.polls.views.detail'),

That means that for all url's that follow that pattern, the detail function in the views.py file will be revoked. If for example the url that is called is 'polls/2/', the function is called like this: detail (request=, poll_id='2')

This is the picture that I found about the issue in http://www.webmonkey.com/tutorial/Use_URL_Patterns_and_Views_in_Django, by designer and Coder Jeff Croft:

However, according to my experience, you could also draw it like this, to visualize how the URL request moves in the Django app:


Thursday, 14 May 2009

Sparks Capturing Learning Django #4

This time I've only got some questions and some new answers... Nothing special. I think I'll have to cut back a bit on the specific questions. It just takes too much time, and it's all Googlable, so it's not really capturing my learning process anyways.

Questions:


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?

Ok, I've understood some more about the hierarchy. Under the project root, you have one views-file, for example views.py, where you add view functions. View functions are such functions that take a request as a parameter and return a HttpResponse. Ok, in correct language its that a view function takes aHttpRequest as a parameter and returns an instance of HttpResponse. Great. [file hierarchy, views, view functions]

5. What is the most basic way to add the url:s to urls.py?

The url:s you add are called URLpattern :s . You add them simply by adding stuff to the urls.py file. First you import the function by "from thesite.nameofviewfile import functionName". So you need to have a view file in the project root (if you create a project called test, the root is at test/ That's also where the urls.py file resides. Then after adding the import you add the following line after the statement "urlpatterns = patterns('', (Which is already in the urls.py file) : ('^functionName/$', functionName), [url, urlpatterns]

6. What is the " '^ " thing that can be seen in the URLpatterns?

That was fast! The caret "^" means: "make sure that the pattern matches the start of the string:" [^]

7. What is the $ sign in the URLpatterns?

The dollar sign means: require that the pattern matches the end of the string. [$]

The dollar and caret signs are used to show what url:s to match to the specified... thing. if you have a caret (^), it means that the url has to start with that. aasch, it's well explained here:

"his concept is best explained by example. If we had instead used the pattern '^hello/' (without a dollar sign at the end), then any URL starting with /hello/ would match, such as /hello/foo and /hello/bar, not just /hello/. Similarly, if we had left off the initial caret character (i.e., 'hello/$'), Django would match any URL that ends with hello/, such as /foo/bar/hello/. If we had simply used hello/, without a caret or dollar sign, then any URL containing hello/ would match, such as /foo/hello/bar. Thus, we use both the caret and dollar sign to ensure that only the URL /hello/ matches — nothing more, nothing less."
(From Djangobook.com) [$,^]

8. If I want to embed an iframe to the app can I do it straight into the HTML?

9. Are all of the URLpatterns always put into the urls.py file?

10. Is it difficult to change to the production server?

11. How do I really get an external CSS sheet working?

How do you install and start developing?

Continuing my Learning Capture Sparks!

1. How do you install?

It's pretty easy to install just using the tutorials that you can find. There's a free Django book at www.djangobook.com. That includes all the info you need. [installing django, django book]

2. How do you start developing?

Django comes with a development server, so you don't have to set up a server and everything. It's easy to follow the tutorials and get the server running and see the administrative interface which is automatically provided by Django. [django start, django server, django setup]
(Problem) I had a problem, I moved around one of the folders which included some django stuff, it was the django-trunk. After that nothing worked and I had to take everything out and try to figure it out again. [problem, django setup, python, django]

To develop:

1. Set up the project.

First set up the project by running the command: django-admin.py startproject nameofyourproject

(If you get an error "-bash: django-admin.py: command not found", your django-admin.py isn't in your system path (don't ask me what that means exactly...). You have to options to remedy the problem:

  1. Faster, but not a long-term solution: instead of django-admin.py write the whole path to where your django installation resides, for example, the whole command to start a project would become: /Home/MyProgramming/django-trunk/django/bin/django-admin.py startproject nameofyourproject
  2. A better solution is to move your django-admin.py to your system path. I got it done by writing: sudo ln -s /Home/MyProgramming/django-trunk/django/bin/django-admin.py /usr/bin/django-admin. After that it worked to just write django-admin.py startproject nameofmyproject to get the project started.
2. Configure the database

Configure the database by adding the necessary information to the nameofproject/settings.py -file. If you're a beginner, just put in 'sq3lite' in the database_engine and define a file for the database in you project in database_name, for example '/Programming/Django/nameofmyproject/mydata.db'

3. Create an app in the project: python manage.py startapp nameofyourapp

This creates an application directory under your project. You have to use apps if you want to use models in Django.

4. Create a model in the models.py file under the app directory

It could look something like this (in the nameofmyproject/nameofmyapp/models.py file):

from

Somewhat answered questions:

3. How do you make graphics with Django?

It seems graphics can be made just by CSS and HTML since Django only adds some stuff to the HTML page. So I'll still have to learn CSS again.

What's Django?

Ok this is where the process starts. It's going to be messy... By the way, my first goal is to be able to create a good looking web page/ web app for my projects. That'll have a blog and static pages for some other stuff. And some tasteful way of displaying my Montreal Events (http://www.dipity.com/Rikuity/Montreal-Tech-Startup/fs),

Django & Python learning [mac os x 10, textmate]

Django is a web framework. A web framework is something that helps you build web applications by automationg tasks. [web framework, django]

Open Questions:

How do you install?
How do you start developing?
How do you make graphics with Django?

The learning process

So this is the first installment of my project to capture my learning process of web development on Django.

First it's important to understand my starting point: I've developed a bit in some different languages (Java, PL/SQL, VB), but never really made a good functioning app. I used to know HTML/CSS pretty well but I've forgotten some stuff about that. Otherwise of course my minor study subject is corporate information systems, so I'm quite familiar with how everything should work. I also like to read alot about web development. I've also been the project manager of a web development project that was done in PHP. We created Facebook, iGoogle etc apps. That's about the most important stuff.

I think for someone who doesn't know anything about coding my learning process might be a bit difficult to grasp. For a coder, who's done that for more than 50 hours in his life, it might be a bit boring, but it'll give you the chance to understand how people who don't know code view it.

I'll be writing about the questions I have, the answers to those questions when I find them and learnings, observations etc. I will not write about everything, because the key to capturing a learning process is not to capture all the small stuff. I'm just going to write down some stuff before and after each learning session.

Shit, this was too long, I'll have to post my first learning capturation (that's a good name!) in another post. doitnow. ok.