Django is a Python web framework that supports rapid development of pragmatic web applications. It allows defining models, views, URLs and templates to structure the application logically. Models define the database schema using Python classes. Views handle requests and return responses like HTML. Templates define presentation logic separately. Django provides a full-featured admin interface and forms to manage data. It aims to strike a balance between high-level features and low-level flexibility.
8. $ ls -l supporter/
__init__.py
manage.py
settings.py
urls.py
9. $ python manage.py runserver
Validating models...
0 errors found
Django version 1.1 beta 1, using
settings 'supporter.settings'
Development server is running at
http://127.0.0.1:8000/
Quit the server with CONTROL-C.
10. settings.py
● DATABASE_ENGINE
● DATABASE_NAME
● DATABASE_USER
● DATABASE_PASSWORD
● DATABASE_HOST
● normál python modul
● a saját konfig paramétereink legyenek
nagybetűsek!
● http://www.djangoproject.com/documentation/settings/
16. BEGIN;
CREATE TABLE "topic_topic" (
"id" integer NOT NULL PRIMARY KEY,
"author_id" integer NOT NULL,
"type" varchar(1) NOT NULL,
"title" varchar(128) NOT NULL,
"content" text NOT NULL,
"feeling" integer NOT NULL
)
;
COMMIT;
18. from django.db import models
from django.contrib.auth.models import User
class Topic(models.Model):
TOPIC_TYPES = (
('Q', 'Question'),
('I', 'Idea'),
('P', 'Problem'),
)
author = models.ForeignKey(User)
type = models.CharField(max_length=1,
choices=TOPIC_TYPES)
title = models.CharField(max_length=128)
content = models.TextField()
feeling = models.IntegerField()
created = models.DateTimeField(auto_now_add=True)
19. $ python manage.py validate
$ python manage.py syncdb
Creating table auth_permission
Creating table auth_group
Creating table auth_user
Creating table auth_message
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table topic_topic
You just installed Django's auth system, which
means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (Leave blank to use 'szferi'):
E-mail address: szferi@wsbricks.com
Password:
Password (again):
Superuser created successfully.
Installing index for auth.Permission model
Installing index for auth.Message model
Installing index for topic.Topic model
21. $ python manage.py shell
In [1]: from supporter.topic.models import Topic
In [2]: from django.contrib.auth.models import User
In [3]: u = User.objects.get(id=1)
In [4]: print u
szferi
In [5]: t = Topics.objects.create(author=u, type='Q',
title='test', content='nagybaj!', feeling=-1)
In [6]: print t
Topic object
In [7]: print t.title
test
In [8]: print Topic.objects.all()
[<Topic: Topic object>]
In [9]: ts = Topic.objects.filter(content__contains='baj')
In [10]: print ts[0].content
nagybaj!
26. from django.db import models
from django.contrib.auth.models import User
class Topic(models.Model):
TOPIC_TYPES = (
('Q', 'Question'),
('I', 'Idea'),
('P', 'Problem'),
)
author = models.ForeignKey(User)
type = models.CharField(max_length=1,
choices=TOPIC_TYPES)
title = models.CharField(max_length=128)
content = models.TextField()
feeling = models.IntegerField()
created = models.DateTimeField(auto_now_add=True)
27. $ python manage.py shell
In [2]: from django.contrib.auth.models import User
In [3]: u = User.objects.get(id=1)
In [4]: print u
szferi
In [5]: print u.topic_set.all()
[<Topic: test>]
In [6]: print u.topic_set.filter(type='Q')
[<Topic: test>]
37. from django.http import
HttpResponse
from supporter.topic.models
import Topic
def index(request):
r = '<ul>'
topics = Topic.objects.all()
for t in topics:
r += "<li>%s</li>" %
t.title
r += '</ul>'
return HttpResponse(r)
46. from models import Topic
from django.forms import ModelForm
class TopicForm(ModelForm):
class Meta:
model = Topic
exclude = ('author', )
47. def add(request):
from forms import TopicForm
from django.http import HttpResponseRedirect
from django.contrib.auth.models import User
if request.method == 'POST':
form = TopicForm(request.POST)
if form.is_valid():
topic = form.save(commit=False)
topic.author = User.objects.get(id=1)
topic.save()
return HttpResponseRedirect('/')
else:
form = TopicForm()
return render_to_response('add.html',
{'form': form })