Web осень 2012 лекция 7
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Web осень 2012 лекция 7

on

  • 206 views

 

Statistics

Views

Total Views
206
Views on SlideShare
203
Embed Views
3

Actions

Likes
0
Downloads
1
Comments
0

2 Embeds 3

https://tech-mail.ru 2
http://tp.mail.ru 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Web осень 2012 лекция 7 Presentation Transcript

  • 1. • • • • •
  • 2. • • • • • • •
  • 3. from django import forms class ContactForm(forms.Form): email = forms.EmailField(label=u'Ваш e- mail', max_length=100) message = forms.CharField(label=u'Сообщение', widget=forms.Tex tarea)
  • 4. >>> f = ContactForm() >>> data = {'email': 'foo@example.com', 'message': 'Hi there’} >>> f = ContactForm(data) >>> f.is_valid() True >>> f.cleaned_data {'email': u'foo@example.com', 'message': u'Hi there’} >>> f = ContactForm({}) >>> f.is_valid() False >>> f.errors {'email': [u'Enter a valid e-mail address.'], 'message': [u'This field is required.']} >>> f = ContactForm() >>> print f.as_table() <tr><th><label for="id_for_email">Ваш e- mail:</label></th><td><input id="id_for_ email " type="text" name="email" maxlength="100" /></td></tr>…
  • 5. from blog.forms import ContactForm from django.core.mail import send_mail from django.http import HttpResponseRedirect def contact(request): if request.method == 'POST': form = ContactForm(request.POST) if form.is_valid(): subject = u'Сообщение с блога' message = form.cleaned_data['message'] sender = form.cleaned_data['email'] recipients = ['a.bekbulatov@corp.mail.ru'] send_mail(subject, message, sender, recipients) return HttpResponseRedirect('/') else: form = ContactForm() return render(request, 'blog/contact.html', { 'form': form })
  • 6. {% extends "base.html" %} {% block content %} <form action="" method="post"> {% csrf_token %} {{ form.as_p }} <input type="submit" value="Отправить" /> </form> {% endblock %}
  • 7. from django import forms class PostForm(forms.ModelForm): class Meta: model = Article fields = ('title', 'content', 'category')
  • 8. • • • • •
  • 9. from django import template from blog.models import Post register = template.Library() @register.inclusion_tag('blog/tags/last_posts.html') def last_posts(): return {'post_list': Post.objects.all()[:5]}
  • 10. <ul> {% for object in post_list %} <li><a href="{{ object.get_absolute_url}}">{{ object }}</a></li> {% endfor %} </ul> {% load blog_tags %} {% last_posts %}
  • 11. • • • • • • • • • • •
  • 12. def post_detail(request, pk): try: object = Post.objects.get(pk=pk) except Post.DoesNotExist: raise Http404 return render( request, 'blog/post_detail.html', {'object': object} ) urlpatterns = patterns('', url(r'^post/(?P<pk>d+)/$', post_detail, name='post_ detail'),
  • 13. class PostDetail(generic.DetailView): model=Post urlpatterns = patterns('', url(r'^post/(?P<pk>d+)/$', PostDetail.as_view(), nam e='post_detail'), )
  • 14. def post_list(request): paginator = Paginator(Post.objects.all(), 25) page = request.GET.get('page') try: posts = paginator.page(page) except PageNotAnInteger: posts = paginator.page(1) except EmptyPage: posts = paginator.page(paginator.num_pages) return render( request, 'blog/post_list.html', {'object_list': posts} )
  • 15. class PostList(generic.ListView): model = Post paginate_by = 25
  • 16. @login_required def category(request, pk): cat = get_object_or_404(Category, pk=pk) post_list = Post.objects.filter(category=cat) return render(request, 'blog/category.html', { 'category': cat, 'post_list' : post_list })
  • 17. class CategoryListView(generic.ListView): template_name = 'blog/category.html' def get_queryset(self): self.cat = get_object_or_404(Category, pk=self.kwargs['pk']) return Post.objects.filter(category=self.cat) @method_decorator(login_required) def dispatch(self, *args, **kwargs): return super(CategoryListView, self).dispatch(*args, **kwargs) def get_context_data(self, **kwargs): context = super(CategoryListView, self).get_context_data(**kwargs) context['category'] = self.cat return context
  • 18. from django.conf.urls import patterns, include, url # Uncomment the next two lines to enable the admin: from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', … # Uncomment the next line to enable the admin: url(r'^admin/', include(admin.site.urls)), )
  • 19. from blog.models import Category, Post from django.contrib import admin class PostAdmin(admin.ModelAdmin): date_hierarchy = 'creation_date' list_display = ('title', 'category') list_filter = ('category',) search_fields = ('title',) admin.site.register(Category) admin.site.register(Post, PostAdmin)
  • 20. • • • • •
  • 21. • • • • • •
  • 22. • • • •
  • 23. • • •
  • 24. • • • • •
  • 25. • • • •
  • 26. • • •
  • 27. • •
  • 28. • • •
  • 29. • • • • •
  • 30. • • • • •