我有一个基于Django类的ListView列表对象。这些对象可以根据位置进行过滤。现在,我希望location ModelChoiceFilter只列出与当前用户相关的位置。相关地点是他拥有的地点。我怎样才能改变查询集?
# models.py
from django.db import models
from django.conf import settings
from rules.contrib.models import RulesModel
from django.utils.translation import gettext_lazy as _
class Locatio
只有在使用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
请帮助澄清一些基本概念。谷歌搜索导致了关于两种不同观点之间的差异的答案,例如,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
我有一个创建多个部门对象的MasterDetailPage。我该怎么做呢?我试着把它绑定到一个标签上,然后从标签上获取数据(我知道这很麻烦),但这是我唯一能想到的事情。
Department[] departments = {
new Department ("D", 1),
new Department ("De", 7),
new Department ("G", 4),
new Department ("M", 9),
我得到了这个错误"'initial‘是这个函数的一个无效关键字参数“,我试图通过createview创建一个对象,这个错误是什么,为什么我要得到它?
urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^add/$', views.CriarTrabalhador.as_view(), name='add')]
models.py
from django.db import models
# Create your mo
我在这里有这样的看法:
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect, HttpResponse
from django.urls import reverse
from django.template import loader
from django.views.generic import ListView
class IndexView(ListView):
def get(self, request):
t
这是一个令人尴尬的简单问题。我正在尝试理解如何在我正在构建的第一个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
未找到页面(404)请求方法: GET请求URL:由: blog.views.like_post引发
没有任何博客与给定的查询匹配。
我为我的博客应用程序做了一个类似的部分,这个错误显示在下面是我的视图、模型和urls文件。
views.py
from django.shortcuts import render,get_object_or_404
from django.views.generic import ListView
from .models import Blog
class BlogsList(ListView):
model=Blog
temp
我已经创建了一个应用程序,它使用slug自动生成一个基于发布的title的。
我已经成功地在ListView中显示了每个帖子,但是链接到DetailView有困难。有什么想法?
我收到以下错误:
Reverse for '/scholarships/test-scholarship-1/' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
模型
from django.db import models
from django.core.
首先,我是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模板中迭代两个上下文
{% for parent, child in all_parents, all_childs %}
我的视图文件是:
class NodeListView(generic.ListView):
model = BackendNodes
context_object_name = 'all_childs'
class BackendModulesListView(generic.ListView):
context_object_name = 'all_parent
我正在做一个django项目,当我尝试运行这个函数时,它返回只能给出一个参数,但只有一个参数。
from django.http import HttpResponse, Http404
from django.template import Context
from django.template.loader import get_template
from django.contrib.auth.models import User
def main_page(request):
template = get_template('main_page.html')
我一直在尝试用django开发一个简单的web应用程序,但我遇到了一个错误。基本上,传递到url中的参数是不被接受的,我得到
TypeError at /NECapp/offers/ offers()恰好接受2个参数(给定1个)
完整跟踪的
应用程序的快速概述:用户输入邮政编码,并获得与该值相关的对象列表。无论我对什么进行修补,代码在这一点上总是会因为不同的错误而中断,所以我将在这里转储大部分代码。由于我已经尝试了很多东西,所以在导入和其他地方可能有不必要的代码行,所以要小心。
views.py
from django.shortcuts import render_to_response
f
问题:我试图通过在浏览器中放置以下url来收回一个详细的视图(单数博客文章):
http://127.0.0.1:8000/post/1/
它应该检索并呈现屏幕上的第一篇博客文章,但是,它会产生以下错误:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/post/1/
Raised by: socialmedia.views.PostDetailView
No post found matching the query
更新,根据丹尼尔罗斯曼的评论如下。我得到的错误是,我没有id 1
我正在学习Django,并且一直在努力使评论形式正确地工作在墙上。基本上,我正在构建的是一个食谱应用程序。这只是实践,但想法是,有人张贴食谱,其他人可以评论它。我有基本的工作,但我不知道如何让重定向回到食谱细节视图后,评论已经提交。
如果我在pk中硬编码它工作,我只需要得到一个pk!
这是我的东西:
Portion of Recipes views.py:
from django.shortcuts import render
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic
我已经定义了模型和观点。我想显示从数据库中存在的项目。但是,脚本没有显示任何内容。问题出在哪里?请查看使用for循环从数据库显示项目的模型、视图和html文件模板的定义。
models.py
from django.db import models
from bifrost.models import CustomUser
# Create your models here.
# Model Projektu
class Project(models.Model):
PROJECT_TYPE = (
('SCR', 'Scrum'),
我有一个过滤器:
class BookFilter(django_filters.FilterSet):
class Meta:
model = Book
fields = '__all__'
和一个ListView来查看结果:
class SearchResultsListView(ListView):
model = Book
context_object_name = 'book_list'
template_name = 'book/search_results.html'
class home(ListView):
template_name='blog/base.html'
这不起作用,产生了这个错误--
ImproperlyConfigured at / home缺少了一个QuerySet。定义home.model、home.queryset或重写home.get_queryset()。
但是当我创建一个没有数据的模型
class home(ListView):
model = Post
template_name='blog/base.html'
这工作得很好
当我不继承->时,ListVie