简体   繁体   English

搜索结果未显示在模板中 (Django)

[英]Search results doesn't show in the template (Django)

I am very new to Django and I am creating a very simple project.我是 Django 的新手,我正在创建一个非常简单的项目。 However, the "search" part of my project is having some issues.但是,我项目的“搜索”部分存在一些问题。 Every time I try to search, it redirect me to the search template but not showing the data from the database.每次我尝试搜索时,它都会将我重定向到搜索模板,但不会显示数据库中的数据。 No error message.没有错误信息。 Here's my code...这是我的代码...

models.py模型.py

class Userprofile(models.Model):
    use_id = models.IntegerField()
    first_name = models.CharField(max_length = 50)
    last_name = models.CharField(max_length = 50)
    position = models.CharField(max_length = 50)
    email = models.CharField(max_length= 100)
    password = models.CharField(max_length= 100)

    def __str__(self):
        return self.first_name

account_list.html This is the template where the search bar is located account_list.html这是搜索栏所在的模板

        <div class="search">
            <form method="post" action="{% url 'account_search' %}" autocomplete="off">
                <br>
                {% csrf_token %}
                <input type="text" name="acc_search" placeholder="Search Account">
                <input type="submit" name="submit" value="Search" style="width: 24%"></p>
            </form>
        </div>
        <hr>

views.py视图.py

def account_search(request):
    if request.method == "POST":
        account_search = request.POST.get('acc_search')
        accounts = Userprofile.objects.filter(use_id__contains=account_search) | Userprofile.objects.filter(first_name__contains=account_search) | Userprofile.objects.filter(last_name__contains=account_search) | Userprofile.objects.filter(position__contains=account_search) | Userprofile.objects.filter(email__contains=account_search)
        return render(request, 'core/account_search.html', {'account_search': account_search, 'accounts':accounts})
    else:
        return render(request, 'core/account_search.html', {})

account_search.html ` account_search.html`

        {% if account_search %}

        <div class="main-right">
            <div class="h-1">
                <h1>'{{ account_search }}' in Accounts</h1>
            </div>

            <table rules="all" style="border: 1px">
                <thead>
                    <td>Personnel's ID</td>
                    <td>Name</td>
                    <td>Position</td>
                    <td>Email</td>
                    <td>Action</td>
                </thead>
                <tbody>
                    {% for userprofile in userprofiles %}
                        <tr>
                            <td>{{ userprofile.use_id }}</td>
                            <td>{{ userprofile.first_name }}
                                {{ userprofile.last_name }}</td>
                            <td>{{ userprofile.position }}</td>
                            <td>{{ userprofile.email }}</td>
                            <td class="action_btn">
                                <a href="account_edit/{{ userprofile.id }}" class="edit_btn">Edit</a>
                                <a href="account_delete/{{ userprofile.id }}" class="delete_btn">Delete</a>
                            </td>
                        </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>

        {% else %}
            <h1 class="h1-else">You forgot to enter the text in the input box.</h1>

        {% endif %}

I've searched in the Google and tried but I can't find any solution or any discussion on how I can fix it.我在 Google 中搜索并尝试过,但找不到任何解决方案或任何关于如何修复它的讨论。

I hope someone can help me, thank you.我希望有人能帮助我,谢谢。 And I'm sorry for my bad English.我很抱歉我的英语不好。

You are passing accounts as context variable from the view, but in template, you are looping through userprofiles .您正在从视图中将accounts作为上下文变量传递,但在模板中,您正在遍历userprofiles Hence change this line in template:因此更改模板中的这一行:

{% for userprofile in userprofiles %}

to

 {% for userprofile in accounts %}

You are passing accounts as "accounts" in the view, but looking for "userprofiles" in the template.您在视图中将帐户作为“帐户”传递,但在模板中查找“用户配置文件”。 You have to use "accounts" in the template as well.您还必须在模板中使用“帐户”。

{% for userprofile in accounts %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM