Django2.0中文(通用视图)

2022-05-14 14:03:49 浏览数 (1)

1、通用视图 urls.py: from django.views.generic import TemplateView path(r'about/',TemplateView.as_view(template_name="about.html")), 2、通用视图 about.html xx/templates/about.html

3、List obj的通用视图 urls.py: from django.views.generic import ListView from blog.models import BlogPost

path(r'bloglist/',ListView.as_view(template_name='bloglist.html',model=BlogPost)), 4、List obj的通用视图bloglist.html:默认list名为object_list {% for dr in object_list %} {{dr.title}}</p> {% endfor %}

5、重写 //ListView:tempate_name/context_object_name/get_queryset()/get_context_data()放其他keys/values进去。

from django.views.generic import ListView class IndexView(ListView): template_name = "blog/index.html" context_object_name = "article_list" model=Article

代码语言:javascript复制
def get_queryset(self):
    article_list = Article.objects.all()
    for article in article_list:
        article.body = markdown2.markdown(article.body, extras=['fenced-code-blocks'], )
    return article_list

def get_context_data(self, **kwargs):
    kwargs['category_list'] = Category.objects.all().order_by('name')
    return super(IndexView, self).get_context_data(**kwargs)

6、DetailView用法: class ArticleDetailView(DetailView): model = Article template_name = "blog/detail.html" context_object_name = "article" pk_url_kwarg = 'article_id'

代码语言:javascript复制
def get_object(self, queryset=None):
    obj = super(ArticleDetailView, self).get_object()
    obj.body = markdown2.markdown(obj.body, extras=['fenced-code-blocks'], )
    return obj

url(r'^article/(?P<article_id>d )$', views.ArticleDetailView.as_view(), name='detail'),

7、CreateView: 默认模板article_form.html 默认context_object_name:form from django.views.generic.edit import CreateView from .models import Article

class ArticleCreateView(CreateView): model = Article fields = ['title', 'body', 'pub_date']

blog/article_form.html

<form method="post">{% csrf_token %} {{ form.as_p }} <input type="submit" value="Save" /> </form> 8、扩展CreateView:自定义form_class/form_valid() from django.views.generic.edit import CreateView from .models import Article from .forms import ArticleCreateForm

class ArticleCreateView(CreateView): model = Article template_name = 'blog/article_create_form.html' form_class = ArticleCreateForm

代码语言:javascript复制
def form_valid(self, form):
   form.do_sth()
   form.instance.author = self.request.user
   return super(ArticleCreateView, self).form_valid(form)

9、UpdateView:默认模板:article_form.html from django.views.generic.edit import UpdateView from .models import Article

class ArticleUpdateView(UpdateView): model = Article fields = ['title', 'body', 'pub_date']

// from django.views.generic.edit import UpdateView from .models import Article from .forms import ArticleUpdateForm

class ArticleUpdateView(UpdateView): model = Article template_name = 'blog/article_update_form.html' form_class = ArticleUpdateForm

代码语言:javascript复制
def form_valid(self, form):
   form.do_sth()
   return super(ArticleUpdateView, self).form_valid(form)

10、DeleteView:默认模板article_confirm_delete.html from django.urls import reverse_lazy from django.views.generic.edit import DeleteView from .models import Article

class ArticleDelete(DeleteView): model = Article success_url = reverse_lazy('index')

blog/article_confirm_delete.html

<form method="post">{% csrf_token %} <p>Are you sure you want to delete "{{ article }}"?</p> <input type="submit" value="Confirm" /> </form>

11、FormView:FormView一般用来展示某个表单,而不是某个模型对象。当用户输入信息未通过表单验证,显示错误信息。当用户输入信息通过表单验证提交后,转到其它页面。使用FormView一般需要定义template_name, form_class和success_url.

views.py - Use FormView

from myapp.forms import ContactForm from django.views.generic.edit import FormView

class ContactView(FormView): template_name = 'contact.html' form_class = ContactForm success_url = '/thanks/'

代码语言:javascript复制
def form_valid(self, form):
    # This method is called when valid form data has been POSTed.
    # It should return an HttpResponse.
    form.send_email()
    return super().form_valid(form)

0 人点赞