我无法解决我的问题,不知道我错在哪里/哪里。这是我的urls.py:
from accounts.views import AccountList as account_views
urlpatterns = [
re_path(r'^account/(?P<uuid>[\w-]+)/', account_views.account_detail, name='account_detail'),
]
这是我的模特
class Account(models.Model):
uuid = ShortUUIDField(unique=True)
name = models.CharField(max_length=80)
desc = models.TextField(blank=True)
address_one = models.CharField(max_length=100)
address_two = models.CharField(max_length=100, blank=True)
city = models.CharField(max_length=50)
state = models.CharField(max_length=2)
phone = models.CharField(max_length=20)
owner = models.ForeignKey(User, on_delete=models.DO_NOTHING,)
created_on = models.DateField(auto_now_add=True)
这是我的观点
class AccountList(ListView):
model = Account
@login_required()
def account_detail(request, uuid):
account = Account.objects.get(uuid=uuid)
if account.owner != request.user:
return HttpResponseForbidden()
variables = {
'account': account,
}
return render(request, 'accounts/account_detail.html', variables)
我试图在我的accaunt_detail模板中使用以下代码渲染它:
{% extends 'base.html' %}
{% block content %}
<div id="content-container" class="container p-none">
<div id="ad-container">
<div id="gi-container" class="ad-container">
<div class="row gi-body">
<div class="col-md-9">
<h5 class="gi-sh">Description</h5>
<p>{{ account.desc }}</p>
</div>
<div class="col-md-3">
<h5 class="gi-sh">Address</h5>
<p class="nm">{{ account.address_one }}</p>
<p class="nm">{{ account.address_two }}</p>
<p class="nm">{{ account.city}}, {{ account.state }}</p>
<p class="nm">{{ account.phone}}</p>
</div>
</div>
</div>
</div>
{# List Contacts #}
{# List Communications #}
</div>
{% endblock %}
我的base.html
{% load static %}
<!DOCTYPE html>
<html>
<head>
<title>CRM Easy</title>
</head>
<body>
<header>
</header>
<div id="site-wrapper">
{% block content %}
if you see this, something is wrong!
{% endblock content %}
</div>
</body>
</html>
我没有收到任何错误,就像一切正常但没有显示用户详细信息。我创建了~15个用户,所以我的数据库中有用户。
答案 0 :(得分:1)
这完全不是基于类的视图的工作原理。您无法定义任意方法并从URL conf指向它们;您始终需要在网址格式中调用as_view()
,dispatch()
方法会调用get()
或post()
。
但是在这种情况下,您没有使用基于类的视图的任何功能,因此使用一个绝对没有意义。您应该从该类中提取account_detail
方法并使其成为独立函数,然后将您的URL模式直接指向该方法。