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

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

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

73

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
73
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

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. • • • • •

×