首页 > 解决方案 > 如何更新通用类视图?

问题描述

我有一个 django 应用程序,可以存储有关不同人员资料的信息。我希望能够提供可下载的电子表格并使用相同的 url 在网站上更新类视图(ListView)。我还没有完全理解类视图,我正在努力弄清楚如何组合下面的两个视图功能。

我试过这个:views.py

class ProfileList(ListView):
    model = Profile
    #template_name = 'search.html'
    def get(self, request):
        return export_profiles(request)

def export_profiles(request):
    # Create the HttpResponse object with the appropriate CSV header.
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename="profile_list.csv"'

    writer = csv.writer(response)
    writer.writerow(['Name','Title','Location','Phone','Email','Company Name','Company Size','Link'])
    data = Profile.objects.filter()
    for row in data:
        rowobj = [row.name,row.title,row.location,row.phone,row.email,row.GetCompany().companyName,row.GetCompany().companySize,row.url]
        writer.writerow(rowobj)
    #Clean up database
    return response

网址.py

urlpatterns = [
    path('search/', views.search, name='search'),
    path('profiles/', ProfileList.as_view()),
    path('accounts/', include('django.contrib.auth.urls')),
    url('session_security/', include('session_security.urls')),
]

这适用于下载文件,但仍然没有使用 django as_view() 函数更新配置文件列表。它只是下载了 csv 文件而没有出错。

这是我目前拥有的:

视图.py

#Post a view of profiles on the website when a search is initiated
class ProfileList(ListView):
    model = Profile
    template_name = 'search.html'  

@login_required
def export_profiles(request):
    # Create the HttpResponse object with the appropriate CSV header.
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename="profile_list.csv"'

    writer = csv.writer(response)
    writer.writerow(['Name','Title','Location','Phone','Email','Company Name','Company Size','Link'])
    data = Profile.objects.filter()
    for row in data:
        rowobj = [row.name,row.title,row.location,row.phone,row.email,row.GetCompany().companyName,row.GetCompany().companySize,row.url]
        writer.writerow(rowobj)

    return response

网址.py

urlpatterns = [
    path('search/', views.search, name='search'),
    path('profiles/', views.export_profiles, name='profiles'),
    path('accounts/', include('django.contrib.auth.urls')),
    url('session_security/', include('session_security.urls')),
]

搜索.html

<div class="column">
            <h3>Profiles</h3>
            <font color="red">
                <p>The following list does not save after the next search you make! Please save the csv file placed in your browser downloads folder.</p>
            </font>
            {% for profile in object_list %}
            <ul>
                <li>Name: {{ profile.name }}</li>
                <li>Title: {{ profile.title }}</li>
                <li>Location: {{ profile.location }}</li>
                <li>Phone: {{ profile.phone }}</li>
                <li>Email: {{ profile.email }}</li>
                <li>Company Name: {{ profile.GetCompany.companyName }}</li>
                <li>Company Size: {{ profile.GetCompany.companySize }}</li>
                <li>Link: <a href={{ profile.url }}>{{ profile.url }}</a></li>
            </ul>
            {% endfor %}
            <br />
        </div>

我尝试在一个 url 下组合一个 django 类视图和另一个视图函数是否正确?一个完整的解决方案会很好,但有很多要求。我只是要求一些指示和链接来进一步教育自己解决这个问题。

标签: djangodjango-modelsdjango-templatesdjango-views

解决方案


推荐阅读