gpt4 book ai didi

python - 如何显示电话簿的详细 View ?

转载 作者:太空宇宙 更新时间:2023-11-03 20:21:11 26 4
gpt4 key购买 nike

我正在尝试显示电话簿中存储的联系人的详细 View 。 PhoneBook(id,name)是我的模型之一,它是模型Contact(id,first_name,last_name,phone_number,phone_book)的外键。

在我的索引页中,有一个打开电话簿的按钮。之后,我希望用户可以单击电话簿,并向他们显示详细 View (名字、姓氏、电话号码)。

在 view.py 中,我有一个函数可以捕获所有电话簿,并通过 context(dict) 传递它。在我的模板中,我使用了 for 循环来遍历所有电话簿并打印它们。

我无法将页面定向到详细 View 。如何获取用户点击的电话簿?以及如何将页面从 ./view 定向到 ./detail

# view.py

def view_phone_book(request):
all_phone_books = PhoneBook.objects.all()

context = {
'all_phone_books': all_phone_books
}

return render(request, "CallCenter/view_phone_book.html", context)

def detailed_view_phone_book(request):
all_contacts = Contact.objects.all().filter(phone_book=phone_book_user_clicked_on)
context = {
'all_contacts': all_contacts
}
return render(request, "CallCenter/detailed_view_phone_book.html", context)
# urls.py
urlpatterns = [
path('', index, name="index"),
path('create/', create_phone_book, name="create"),
path('add/', add_to_phone_book, name="add"),
path('view/', view_phone_book, name="view"),
path('detail/', detailed_view_phone_book, name="detailed_view")
]
# models.py
class PhoneBook(models.Model):
"""
Model to store customer to a phone book
"""

name = models.CharField(max_length=10, blank=False)

def __str__(self):
return self.name

class Contact(models.Model):

"""
Model to store customer to a phone book.
"""

first_name = models.CharField(max_length=50, blank=False)
last_name = models.CharField(max_length=50, blank=False)
phone_number = models.CharField(max_length=13, blank=False, unique=True)
phone_book = models.ForeignKey(PhoneBook, on_delete=models.CASCADE)

def __str__(self):
return self.phone_number
<!--view_phone_book.html-->
<table>
<tr>
<th>Phone Book</th>
</tr>

{% for phone_book in all_phone_books %}
<tr>
<form method="get" action="../detail/"><td><a href="">{{ phone_book }}</a> </td></form>
</tr>

{% endfor %}
</table>


<!--detailed_view_phone_book.html-->
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Phone Number</th>
</tr>
{% for phone_detail in all_phone_detail %}
<tr>
<form>
<td>{{ phone_detail.first_name }}</td>
<td>{{ phone_detail.last_name }}</td>
<td>{{ phone_detail.phone_number }}</td>
</form>
</tr>
{% endfor %}
</table>

我无法从 ./view 转到 ./detail。另外,我如何知道用户点击了哪个电话簿?

最佳答案

我想出了如何让它发挥作用,并且我正在回答它,以便如果有人陷入困境,它可以帮助自己。

# views.py 

def view_phone_book(request):
all_phone_books = PhoneBook.objects.all()

context = {
'all_phone_books': all_phone_books
}

return render(request, "CallCenter/view_phone_book.html", context)

def detailed_view_phone_book(request, phone_book_id):
try:
all_contacts = Contact.objects.filter(pk=phone_book_id)
except Contact.DoesNotExist:
raise Http404("PhoneBook Does Not Exist!")
context = {
'all_contacts': all_contacts
}
return render(request, "CallCenter/detailed_view_phone_book.html", context)

#urls.py 

urlpatterns = [
path('', index, name="index"),
path('create/', create_phone_book, name="create"),
path('add/', add_to_phone_book, name="add"),
path('campaign/', create_campaign, name="create-campaign"),
path('view/', view_phone_book, name="view-phone-book"),
path('detail/<int:phone_book_id>', detailed_view_phone_book, name="detail-view-phone-book"),
<!--view_phone_book.html-->
<body>
{% for phone_book in all_phone_books%}
<a href="{% url 'detail-view-phone-book' phone_book.id%}">{{ phone_book }}</a>
<br>
{% endfor %}

<a href="{% url 'index' %}">Back To Home</a>
</body>


<!--detailed_view_phone_book.html-->
{% if all_contacts %}
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Phone Number</th>
</tr>
{% for contact in all_contacts %}
<tr>
<form>
<td>{{ contact.first_name }}</td>
<td>{{ contact.last_name }}</td>
<td>{{ contact.phone_number }}</td>
</form>
</tr>
{% endfor %}
</table>
{% endif %}


<a href="{% url 'index' %}">Back To Home</a>

我观看了Brain's CS50 video ,这对我有帮助。我建议你也这样做。他以适合初学者的方式解释了这些概念。

关于python - 如何显示电话簿的详细 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58150243/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com