请帮助澄清一些基本概念。谷歌搜索导致了关于两种不同观点之间的差异的答案,例如,ListView与DetailView之间的区别,但并不是我的困惑所在:
正式的Django文档有两个导入版本:
from django.views import generic
class IndexView(generic.ListView):
和
from django.views.generic.detail import DetailView
class ArticleListView(ListView):
这两个可以互换吗?我有以下几点,似乎没有不良影响:
from django.views import
我正在尝试使用ListView的Django分页器,但是分页器显示的是文章标题而不是页码:Page number replaced with post title 我怎么才能修复它呢?这是我的代码:在views.py中 from django.shortcuts import render
from django.core.paginator import Paginator
from django.views.generic import ListView, DetailView
from .models import Post
# Create your views here.
#
我在view.py上有一个view.py类
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.views import generic
from entertainment.models import Entertainmentblog
class ListView(generic.ListView):
templat
我检查了这个网站上的所有问题和答案,但没有找到解决方案。我是Django的新手,正在尝试开发一个存储模型,并且已经在ListView中使用过了。
我的view.py的一部分:
from django.shortcuts import render, redirect
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from Stor
我想从团队模型中添加/删除成员。成员指定为ManyToManyField。我使用django-规则来指定权限,所以团队所有者应该能够添加/删除成员。
# models.py
from django.db import models
from rules.contrib.models import RulesModel
from django.conf import settings
class Team(RulesModel):
name = models.CharField(max_length=80)
owner = models.ForeignKey(
只有在使用ListView中的作者名称时,才应该显示作者的书籍。
models.py
from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse
from django.utils.text import slugify
class Author(models.Model):´
author = models.ForeignKey(User, on_delete=models.CASCADE)
slug = models.Slu
在这里创建你的模型。
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
class Post(models.Model):
STATUS_CHOICES = (('draft', 'Draft'), ('published', 'Published'),)
title = models.CharField(max_length=250)
sl
在正式的Django教程中,我遇到了一个问题,无法检查len(Question.objects.all())是否等于0。我一直在想这个问题:
Could not parse the remainder: '(Question.objects.all())' from 'len(Question.objects.all())'
我不明白为什么会这样。
投票/模板/投票/Details.html
<h1> {{ question.question_text }} </h1>
{% if error_message %}<p> &
这是一个令人尴尬的简单问题。我正在尝试理解如何在我正在构建的第一个Django应用程序中集成一个简单的Python函数。这是我的views.py文件...
from django.shortcuts import render
from noticeboard.models import Listings
from django.views import generic
from django.template import Context
class IndexView(generic.ListView):
template_name = 'listings/index.h
我正在制作django博客,并希望为每个博客文章显示一个评论列表,但我很难找到如何在视图和模板中引用评论。我的模型定义如下:
class Issue(models.Model):
title = models.CharField(max_length=255)
text = models.TextField()
author = models.ForeignKey(User)
def __unicode__(self):
return self.title
class Comment(models.Model):
commenter
在Django中,我试图在同一个模型上使用一个ListView来处理多个URL。具体来说,我有一个使用ListView和Pagination发布博客文章的主页,我希望有一个使用没有pagination的ListView的存档页面,并有一个不同的设计。所以我试着这么做:
urls.py:
from django.conf.urls import patterns, url
from django.views.generic import ListView, DetailView
from blogengine.models import Post
urlpatterns = patterns(
在我的观点中,我没有提到template_name,那么django将如何决定在请求期间呈现哪个html页面?
这是我的views.py:
from django.shortcuts import render
from django.views.generic import ListView, DetailView
from . models import Movie
class MovieList(ListView):
model = Movie
# template_name = 'movie_list.html'
class MovieDetail
我想换我的贴身模特。在我有一个非常简单的关系之前。有User、Post和Group三种模式。用户可以创建post,而post必须属于1组。用户Samir写了一篇关于足球的文章。这个职位属于团体体育。下面是模型的外观
早期模型:
class Post(models.Model):
user = models.ForeignKey(User, related_name='posts')
group = models.ForeignKey(Group, related_name='posts')
title = models.CharField
我正在学习Django教程(第4部分- ),需要导入以下内容:
from django.views.generic import DetailView, ListView
我使用此导入行复制了代码片段,但PyDev将DetailView和ListView下划线显示为未解决的导入。我打开模块django.views.generic,看到它唯一包含的内容是:
class GenericViewError(Exception):
"""A problem in a generic view."""
pass
既不是DetailView,
首先,我是Django世界的初学者,所以可能我尝试做的事情是疯狂的,或者是对概念的误解,所以我们开始:
如何查看,泛型视图为我们在模板中一直使用常见模式节省了大量时间,其中之一是根据一组属性检索模型列表的ListView
from models import Post
from django.views.generic import ListView, DetailView
class PostListView(ListView):
model = Post
template_name = 'blog/post_list.html
我正在学习Django网络框架,从过去的几天和它相当棒。我正在学习以课堂为基础的视图来显示内容。我创建了一个简单的示例模型school(name,principal,location)和student(name,age,school(foreign key))。模型是
from django.db import models
# Create your models here.
# Create a School model with different classes
# School Model with Name,Pricipal & Location
class Schoo
我正在构建一个web应用程序,我正在尝试将基于类的视图中的查询集作为变量传递给Django 2.0中的模板。但这些变量没有在上显示。其他文本将显示在此url路由上,但queryset对象不会在页面上显示为变量。models.py和数据库工作正常,数据库中填充了大约10k条记录。我遗漏了什么?简单地说,我还想知道基于类的视图相对于基于函数的视图的优势。谢谢。我的views.py
from django.http import HttpResponse
from django.template.response import TemplateResponse
from django.views.g
每当我尝试链接Edit (article_edit.html)和Delete (article_detail.html)时,Django都会显示这个错误:
NoReverseMatch at /articles/3/
Reverse for 'article_edit' with arguments '('',)' not found. 1 pattern(s) tried: ['articles/(?P<pk>[0-9]+)/edit/$']
Request Method: GET
Request URL: h
我在django应用程序中使用了基于类的视图的混合器。我还创建了一个名为“指导员”的组,只允许属于该组的用户具有编辑、添加和删除课程对象的特定权限。在负责呈现当前用户创建的课程的ManageCourseList View中,我试图重写方法以传递一个额外的'group_list‘参数,该参数包含用户所属的组。我试图在模板ManageCourseList视图呈现中访问它。问题是,虽然当前用户属于指导员组,但仍然呈现其他条件数据。请帮忙,我的档案:
Views.py
from django.shortcuts import render
from django.core.urlresolve
考虑一下这个cms_app.py
from django.utils.translation import ugettext_lazy as _
from cms.app_base import CMSApp
from cms.apphook_pool import apphook_pool
class FooHook(CMSApp):
name = _("FooHook Plugin")
urls = ["foo.urls"]
apphook_pool.register(FooHook)
foo应用程序有一个视图模型,其中包含默认的dja
我有一个django应用程序,它有一个叫做文件夹的模型,用户可以创建一个带有名称字段的文件夹。我想要的是使这个文件夹模型嵌套,这样用户就可以在文件夹中创建文件夹。例如,用户创建了一个名为ABC的文件夹,并在该文件夹的DetailView中创建了另一个文件夹,然后进入DEF,创建了另一个名为GHI的文件夹,依此类推。
我对django非常陌生,我把搜索捆绑在网络上,但一无所获。
models.py
from django.db import models
from django.contrib.auth.models import User
from django.db.models.delet
我使用django详细信息视图显示单个公司详细信息。但是现在我想在同一模板列表上显示该公司员工的用户列表。但我使用的是用户Auth表。如何显示多个视图,如公司的详细信息视图和该公司用户的列表视图。
class CompanyDetailView(LoginRequiredMixin, generic.DetailView):
model = Company
template_name = 'company/company-detail.html'
context_object_name = 'companies'
class UserL
我按照django文档做了一个简单的投票应用。我遇到了以下错误:
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^polls/
^admin/
The current URL, , didn't match any of these."
settings.py
ROOT_URLCONF = 'mysite.urls'
mysite/mysite/urls.py
from django.conf.urls impor
from django.shortcuts import render
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from journal.models import Post, Corrections, Comments
class PostListView(ListView):
model = Post
def get_context_data(self, **kwargs):
context = s
在Createview类中的Django应用程序中,它从不输入is_valid():语句,而且我似乎找不到任何错误:
models.py
from django.db import models
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from django.conf import settings
from .validators import validate_file_extension
import zipfile
class Post(models
我在Django 1.11中有一个ListView模板,它呈现给group_list.html。但是,我希望在主页的列中显示相同的视图。
下面还有截图,如果这有助于理解我想要实现的目标的话
我尝试过不同的方法来使用{% include 'some.html' %} 1),我尝试制作一个html页面并将它包含在主页中。2)我试图将group_list.html页面更改为一个(插入模板),并尝试将其插入索引页中,并为组创建了另一个模板视图。但这也带来了错误
Below are my views and templates
Views.py
class GroupCreate(Lo