简体   繁体   English

如何将数据库查询集对象从基于类的视图(类 SignUp(generic.CreateView))传递到 Django 中的模板

[英]How to pass database queryset objects from class based views(class SignUp(generic.CreateView)) to templates in Django

views.py视图.py

from django.shortcuts import render
from django.urls import reverse_lazy
from django.views import generic
from .forms import CustomUserCreationForm

class SignUp(generic.CreateView):
    form_class = CustomUserCreationForm
    success_url = reverse_lazy('login')
    template_name = 'signup.html'

urls.py网址.py

urlpatterns = [
    url(r'signup/', views.SignUp.as_view(), name='signup'),
    url(r'^$', TemplateView.as_view(template_name='home.html'), name='home'),
]

I used django custom user creation method to sign up users, its working fine.我使用 django 自定义用户创建方法来注册用户,它工作正常。 But how to pass objects to templates in my class SignUp.但是如何将对象传递给我的类 SignUp 中的模板。 I'm new to class based view.我是基于类的视图的新手。 Please help me.请帮我。

To pass context, you can write要传递上下文,您可以编写

context_object_name = 'username'

Now think like you are making a To Do app and want to print the Tasks as per their You can then do the following inside your class based view,现在想想你正在制作一个待办事项应用程序并希望根据他们的任务打印任务然后你可以在基于类的视图中执行以下操作,

def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['tasks'] = context['tasks'].filter(user=self.request.user)
        context['count'] = context['tasks'].count()

In this way, you can write the database queryset in class based views.通过这种方式,您可以在基于类的视图中编写数据库查询集。

If you want a perfect guide or source code to learn the django authentication ie login and logout user functions, I suggest you to go once through this tutorial, It is a todo list app which will cover your doubts如果你想要一个完美的指南或源代码来学习 django 身份验证,即登录和注销用户功能,我建议你通过本教程一次,这是一个待办事项列表应用程序,它将覆盖你的疑虑

To Do list app with django authentication and queryset in class based views 在基于类的视图中使用 django 身份验证和查询集列出应用程序

The sourcecode for the above is here:上面的源代码在这里:

Source code 源代码

You need to override get_context_data method in SignUp class, like this: 您需要在SignUp类中重写get_context_data方法,如下所示:

class SignUp(generic.CreateView):
    form_class = CustomUserCreationForm
    success_url = reverse_lazy('login')
    template_name = 'signup.html'

    def get_context_data(self, **kwargs):
       context = super(SignUp, self).get_context_data(**kwargs)
       context['your_qset'] = YourModel.objects.all()
       return context

and use it in template: 并在模板中使用它:

{% for obj in your_qset %}
   {{ obj }}
{% endfor %}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据库查询集对象从基于类的视图传递到 Django 2.0 中的模板 - How to pass database queryset objects from class based views to templates in Django 2.0 Django:将URL参数传递给generic.CreateView中的context_data - Django: Pass URL parameter to context_data in generic.CreateView Django的通用视图:如何根据外部类属性过滤get_queryset? - Django's Generic Views: how to filter get_queryset based on foreign class attributes? Django:尝试了解queryset属性在基于类的通用视图中如何工作 - Django: trying to understand how the queryset attribute works in class-based generic views 如何在 Django 中使用基于 Class 的视图制作注册视图? - How to make a signup view using Class based views in Django? 基于类的视图中的Django绑定窗体-CreateView类 - Django Bounded forms in Class based views - CreateView class 如何在 class CreateView (django.views.generic) 中设置 select 日期为截止日期? - How do you set select date in class CreateView (django.views.generic) for deadline? Django url未在基于类的视图中使用CreateView查找模板 - Django url not finding a template using CreateView in Class-Based Views DJango CreateView未设置基于类的视图的数据库密钥 - DJango CreateView not setting DB Key with Class-Based Views 基于django类的通用视图“ CreateView”唯一的段错误处理 - django class based generic view “CreateView” unique slug error handeling
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM