Skip to main content
 首页 » 编程设计

python之将函数 View 转换为基于类的 View Django

2024年09月03日37fff_TT

我正在尝试将基于函数的 View (FBV) 编写为基于类的 View (CBV),特别是 CreateView。

到目前为止,我已经创建了基于类的 View ,但是我使用的 FBV 需要一个请求和一个 ID,所以不确定如何处理它。

FBV 工作正常,但作为 CBV,我认为它更复杂,因为我需要更改传递给 HTML 的数据

我认为我不应该使用上下文,但我不知道如果没有上下文该怎么做

感谢您的帮助

FBV

def pages(request, id): 
 
   obj = programas.objects.get(id=id) 
   script = obj.script 
   script_eng = obj.script_eng 
   zip_scripts = zip(script , script_eng) 
   zip_scripts_eng = zip(script_eng , script) 
   random_zip = list(zip(script , script_eng)) 
   random_ten = random.choices(random_zip) 
 
 
 
   context = {'title': obj.title, 
              'show_date': obj.show_date, 
              'script' : obj.script, 
              'script_eng': obj.script_eng, 
              'description': obj.description, 
              'description_eng': obj.description_eng, 
              'show_id':obj.show_id, 
              'url': obj.url, 
              'random_ten': random_ten, 
              'zip_scripts' : zip_scripts, 
              'zip_scripts_eng ' : zip_scripts_eng , 
               } 
 
 
   return render(request, 'rtves/pages.html', context) 

生物转化率

class PagesContentView(ListView): 
   model = programas 
   context_object_name = "show_info" 
   template_name = 'pages/index.html' 
 
   def pages(request, id): 
 
          obj = programas.objects.get(id=id) 
          script = obj.script 
          script_eng = obj.script_eng 
          zip_scripts = zip(script , script_eng) 
          zip_scripts_eng = zip(script_eng , script) 
          random_zip = list(zip(script , script_eng)) 
          random_ten = random.choices(random_zip) 
 
 
 
          context = {'title': obj.title, 
                     'show_date': obj.show_date, 
                     'script' : obj.script, 
                     'script_eng': obj.script_eng, 
                     'description': obj.description, 
                     'description_eng': obj.description_eng, 
                     'show_id':obj.show_id, 
                     'url': obj.url, 
                     'random_ten': random_ten, 
                     'zip_scripts' : zip_scripts, 
                     'zip_scripts_eng ' : zip_scripts_eng , 
                      } 
 
 
          return render(request, template_name, context) 

URLS 工作正常

urlpatterns = [ 
path('about/', views.AboutView.as_view()), 
path('', views.IndexView.as_view()),     
path('pages/<int:id>/', PagesContentView.as_view()), 

]

页面加载正常,但没有从数据库返回任何数据。

HTML

{% if show_info %} 
 
<h2>{{ title }}</h2> 
<p>{{ description_eng | truncatewords_html:100 | safe }}</p> 
    <p> Number of words: {{ script |wordcount }} </p> 
 
{% endif %} 
 
 
{% for rand in random_ten %} 
    <p style="padding: 20px;text-align: left;color:#3d6cdd; line-height: 1.3;">  
    {{ rand.0 |truncatewords:30 }}</p> 
{% endfor %} 

如果我使用 {{ show_info }} 或 {{ show_info.0 }} 我会得到函数“title”第一行返回的查询集:obj .title,但与 ID 不匹配

请您参考如下方法:

这里不是ListView,而是DetailView。您可以将其实现为:

from django.views.generic import <b>DetailView</b> 
import random 
 
class PagesContentView(<b>DetailView</b>): 
    model = programas 
    context_object_name = 'obj' 
    pk_url_kwarg = 'id' 
    template_name = 'pages/index.html' 
 
    def get_context_data(self, *args, **kwargs): 
        context = super().get_context_data(*args, **kwargs) 
        zip_scripts = list(zip(self.object.script , self.object.script_eng)) 
        context.update( 
            zip_scripts=zip_scripts 
            zip_scripts_eng = zip(self.object.script_eng , self.object.script) 
            random_ten=random.choices(zip_scripts) 
        ) 
        return context

因此,我们在这里指定 URL 路径中的主键是 'id',而不是 'pk',并且我们将对象作为 '对象'.

在您的模板中,您可以使用以下方式呈现它:

<h2>{{ <b>obj.</b>title }}</h2> 
<p>{{ <b>obj.</b>description_eng | truncatewords_html:100 | safe }}</p> 
    <p> Number of words: {{ <b>obj.</b>script |wordcount }} </p> 
 
{% endif %} 
 
 
{% for rand in random_ten %} 
    <p style="padding: 20px;text-align: left;color:#3d6cdd; line-height: 1.3;">  
    {{ rand.0 |truncatewords:30 }}</p> 
{% endfor %}

请注意,random_ten 将包含一个二元组,而不是二元组的可迭代对象。您可能正在寻找 random.sample function [Python-doc]相反。

模型通常在 CamelCase 中有单数名称,因此您可以考虑将模型重命名为 Program,而不是 programmas。在 URL 路径中,主键通常被命名为 pk,而不是 id。通过这样做,您可以删除 pk_url_kwargs = 'id' 行。

最后,按照模板中的规定,您通常不会单独传递每个对象属性,而只是传递一个对象,并在模板中渲染该对象。