首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NoReverseMatch错误Django有人能解释一下这些网址是怎么失败的吗?

NoReverseMatch错误Django有人能解释一下这些网址是怎么失败的吗?
EN

Stack Overflow用户
提问于 2021-07-10 12:02:05
回答 2查看 33关注 0票数 0

有人能给我解释一下这些urls在django中是如何工作的吗?我正在尝试为twitter克隆创建一个跟随/取消跟随功能,跟随功能正在工作,但当我使用取消跟随功能时,我得到了以下错误。有人能给我解释一下为什么这个失败了吗?我还在学习django,我想知道为什么它不能工作。

找不到关键字参数为‘{’NoReverseMatch‘:’fake_account‘}的'foxyprofile’在/u/fake_account/unfollow/ Reverse处的用户名。尝试了1个模式:['u/(?P^/+)/$']

这是我的代码,如果你需要更多,请告诉我。

foxyprofile.html

代码语言:javascript
复制
{% extends 'core/base.html' %} {% load humanize %} {% block content %}
<div class="container">
  <div class="columns">
    <div class="column is-12">
      <h1 class="title">{{ user.username }}</h1>
      <p><a href="{% url 'followers' user.username %}"> Followers: {{ user.foxyprofile.followed_by.count }}</p></a>
      <p><a href="{% url 'following' user.username %}"> Follows {{ user.foxyprofile.follows.count }}</p></a>
      <hr />
      {% if user != request.user %} 
        {% if request.user.foxyprofile in user.foxyprofile.followed_by.all %}
            <a href="{% url 'unfollow_foxy' username=user.username %}" class="button is-danger">Unfollow {{ user.username}}</a>
        {% else %}
            <a href="{% url 'follow_foxy' user.username %}" class="button is-success">Follow {{ user.username}}</a>
        {% endif %} 
      {% endif %}
    </div>
  </div>
  <div class="columns">
    <div class="column is-8">
      <div class="wrapper-fox">
        {% for fox in user.foxs.all %}
        <div class="fox">
          <p class="name">{{ fox.created_by.username }}</p>
          <p>{{ fox.body }}</p>
          <p class="info">{{ fox.created_at|naturaltime}}</p>
          <hr />
          {% endfor %}
        </div>
      </div>
    </div>
  </div>
</div>
{% endblock %}

Views.py

代码语言:javascript
复制
from django.shortcuts import redirect, render, get_object_or_404
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
# Create your views here.


def foxyprofile(request, username):
    user = get_object_or_404(User, username=username)

    context = {
        'user': user
    }
    return render(request, 'foxyprofile/foxyprofile.html', context)


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

    request.user.foxyprofile.follows.add(user.foxyprofile)

    return redirect('foxyprofile', username=username)


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

    request.user.foxyprofile.follows.remove(user.foxyprofile)

    return redirect('foxyprofile', usersname=username)



def followers(request, username):
    user = get_object_or_404(User, username=username)

    return render(request, 'foxyprofile/followers.html', {'user': user})

def following(request, username):
    user = get_object_or_404(User, username=username)

    return render(request, 'foxyprofile/following.html', {'user': user})

urls.py

代码语言:javascript
复制
from apps.feed.views import feed, search
from apps.feed.api import api_add_fox
from django.urls import path, include
from apps.foxyprofile.views import foxyprofile, follow_foxy, unfollow_foxy, unfollow_foxy_tiny, follow_foxy_tiny, followers, following
from . import views
from django.contrib.auth import views as v

urlpatterns = [
    path('', views.homepage, name='homepage'),
    path('signup/', views.signup, name='signup'),
    path('logout/', v.LogoutView.as_view(), name='logout'),
    path('login/', v.LoginView.as_view(template_name='core/login.html'), name="login"),
    path('feed/', feed, name='feed'),
    path('api/add_fox/', api_add_fox, name='api_add_fox'),
    path('search/', search, name='search'),
    path('u/<str:username>/', foxyprofile, name='foxyprofile'),
    path('u/<str:username>/follow/', follow_foxy, name='follow_foxy'),
    path('u/<str:username>/unfollow/', unfollow_foxy, name='unfollow_foxy'),
    path('u/<str:username>/followers/', followers, name='followers'),
    path('u/<str:username>/following/', following, name='following'),
]
EN

回答 2

Stack Overflow用户

发布于 2021-07-10 12:53:53

我认为这个错误来自html文件中的这一行:

代码语言:javascript
复制
<a href="{% url 'unfollow_foxy' username=user.username %}" class="button is-danger">Unfollow {{ user.username}}</a>

尝试将其更改为:

代码语言:javascript
复制
<a href="{% url 'unfollow_foxy' user.username %}" class="button is-danger">Unfollow {{ user.username}}</a>
票数 1
EN

Stack Overflow用户

发布于 2021-07-10 13:32:08

您应该在unfollow_foxy的views.py中尝试此操作

代码语言:javascript
复制
from django.urls import reverse

# your code

return redirect(reverse('foxyprofile'), usersname=username)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68324585

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档