没有用户匹配给定的查询.找不到页面404? [英] No User matches the given query. Page Not found 404?

查看:49
本文介绍了没有用户匹配给定的查询.找不到页面404?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的博客网站中,我想通过单击其中一个链接来打开另一个用户的个人资料.但是它继续显示404错误,提示没有用户匹配给定的查询."

In my blog website I want to open another user's profile on clicking one of the links. But it keeps on showing 404 error saying No user matches the given query.

这是 base.html

<a href="{% url 'blogapp:userprofile' username=view.kwargs.username %}">{{ view.kwargs.username }}</a>

这是该函数的 urls.py 模式-

path('userprofile/<str:username>/',views.userprofile,name='userprofile'),

这是我的函数 views.py

@login_required
def userprofile(request,username):
    user = get_object_or_404(User,username='username')

    return render(request,'blogapp/userprofile.html',{'user':user})

这是我的模板文件

{% extends 'blogapp/base.html' %}

{% block content %}

          <div class="row">
            <img  src="{{ user.profile.image.url }}">
          <div class="details">
            <h2>{{ user.username }}</h2>
            <p>{{ user.email }}</p>
          </div>
          </div>
          <p style="margin-top: 10px; margin-left: 138px;">{{ user.profile.description }}</p>
          <hr>


{% endblock %}

{{view.kwargs.username}} 提供了我要搜索的完美用户名.但是问题出在用户配置文件视图中.它可以到达完美的路由 http://127.0.0.1:8000/userprofile/username/,但仍显示404错误.

{{ view.kwargs.username }} gives the perfect username that I search for. But problem is somewhere in the userprofile view. It goes to the perfect route http://127.0.0.1:8000/userprofile/username/ but it still shows a 404 error.

推荐答案

在您看来,您是将 username 作为字符串传递,而它应该是在 userprofile中传递的变量函数.

In your views you are passing username as a string, whereas it should be a variable that's being passed inside the userprofile function.

@login_required
def userprofile(request,username):
    user = get_object_or_404(User,username=username)

    return render(request,'blogapp/userprofile.html',{'user':user})

这篇关于没有用户匹配给定的查询.找不到页面404?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆