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

Saturday 30 May 2009

How do I take the admin interface into use?

So now I'll be tackling my question nr 15. The admin interface

15. How do I take the admin interface into use?

1.Settings.py -file

You have to comment out the necessary lines in your settings.py file under /djangoproject or whatever your djangoproject is called.

The lines you need are:

In INSTALLED_APPS, uncomment

'django.contrib.admin'
'django.contrib.auth',
'django.contrib.contenttypes'
'django.contrib.sessions'

In MIDDLEWARE_CLASSES uncomment
'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware' 'django.contrib.auth.middleware.AuthenticationMiddleware'

2. Run python manage.py syncdb

This creates the database tables you need for the admin interface when you have uncommented the lines above.

If it's the first time you do this, you'll be asked to create a superuser. Do it and remember your password. Otherwise you'll be able to use the password you have created before. I've actually created the super user for another application, I think, but it still works. Weird. Maybe I'll have an answer for why that happens later. Oh, right, it uses the same database now I guess as for the other applications? Ok, whatever.

Now, if you have already created your own models and you want them to show up in your admin interface, you have to add an admin.py file under your app folder (for the application that you want the models to show up in the admin interface), it should look something like this for a basic setup:

from django.contrib import admin
from yourproject.yourappname.models import NameOfclass1, NameOfclass2

admin.site.register(NameOfclass1)
admin.site.register(NameOfclass2)

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?

Thursday 21 May 2009

Sparks Capturing Learning Django #6

So I'm back at the Django Learning process documentation again...

Something important:

The Django Website is a project that can contain a lot of apps. The apps are the ones that contain some specific funtionality. They are separate from the project to be able to be reused.
So whenever you are creating a new functionality into your site, you make it an app. Or should. The apps reside in your project folder: .../djangoproject/app ... Into the app folder you get all the usual stuff for an app... __init__.py , views.py, models.py and tests.py ... Then you just build a new app under that /djangoproject/app folder.

New questions:

12. How do I connect a new app that I've created under the project folder to the project?

13. What's the development process if I want to create a new website with apps?

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.

Capturing the Learning Process - Example Django

From my experience as a teacher and learner, I've noticed that information actually is not the basis of knowledge. You can never just show someone how to solve a maths problem. They'll just forget it. What you have to do is the help the student to realize how to solve it. Usually it helps to ask the right questions for example. So that the student can have an epiphany and suddenly get it. Feels great.

Well, that's the basis of my Project of Capturing Learning Processes. I don't have a proper name for it yet though. This is a project that's going to take a while to conceptualize, since I want to revolutionize learning from the starting point of identifying the problem. I'll call the project sparks for now.

Problem: There is a lot of knowledge lost in learning. Learning is inefficient. Let's make learning more efficient by using the web.

I'm not going to tell more about the project right now, until I get a bit further. For now, I'm going to settle on Capturing my own Learning Process. I'm going to learn web developement on Django.