django - 如何更新通用类视图?
问题描述
我有一个 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 类视图和另一个视图函数是否正确?一个完整的解决方案会很好,但有很多要求。我只是要求一些指示和链接来进一步教育自己解决这个问题。
解决方案
推荐阅读
- postgresql - 无法创建绽放索引
- javascript - 我正在尝试从数据库映射 mydata,但出现此错误。TypeError:无法读取未定义的属性“地图”
- swift - Swift:NSCollectionViewItem 未显示在 NSViewController 中
- c++ - 为什么即使它需要一个右值引用,也可以将左值传递给`std::async`?
- macos - 使用 Applescript 或 Automator 编辑日历以将所有事件“向前移动”一天
- authentication - 单次登录即可访问多个 API(内部和外部)
- reporting-services - SSRS 2016 报告生成器显示 VPN 访问问题
- android - 如何通过动态调整大小的应用栏保持偏移?
- c# - 加密 UWP 应用中的文件
- javascript - JavaScript传参错误,点击事件传中所有参数