【问题标题】:NoReverseMatch Django url issueNoReverseMatch Django url问题
【发布时间】:2018-07-01 05:32:21
【问题描述】:

我正在尝试在我的 django 项目中实现一个典型的 url,但是我不断收到错误消息。我检查了我在 KWARGS 中传递的网址、视图和 html 代码。我不知道这里出了什么问题,请帮忙?

home.html 在模板中使用 user_profile_detail

userprofile/home.html

<div class="sidebar-fixed position-fixed side_nav_bar ">

    <a class="logo-wrapper waves-effect">
        <img src="/" class="img-fluid" alt="">
    </a>

    <div class="list-group list-group-flush">
        **<a href="{% url 'userprofile:dashboard' user_profile_detail.slug %}" class="list-group-item {% if request.path == dashboard %}active{% endif %} waves-effect">
            <i class="fa fa-pie-chart mr-3"></i>Dashboard
        </a>**

        <a href="{% url 'userprofile:profile' user_profile_detail.slug  %}" class="list-group-item {% if request.path == profile %}active{% endif %} list-group-item-action waves-effect">
            <i class="fa fa-user mr-3"></i>Profile</a>

        <a href="{% url 'userprofile:watchlist' user_profile_detail.slug  %}" class="list-group-item {% if request.path == watchlist %}active{% endif %} list-group-item-action waves-effect">
            <i class="fa fa-eye mr-3" aria-hidden="true"></i>WatchList</a>

        <a href="{% url 'userprofile:blog' user_profile_detail.slug  %}" class="list-group-item {% if request.path == blog %}active{% endif %} list-group-item-action waves-effect">
            <i class="fa fa-book mr-3" aria-hidden="true"></i>My Blogs</a>

        <a href="{% url 'userprofile:history' user_profile_detail.slug  %}" class="list-group-item {% if request.path == history %}active{% endif %} list-group-item-action waves-effect">
            <i class="fa fa-history mr-3" aria-hidden="true"></i>My Browsing History</a>
    </div>

</div>

MODELS.PY

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    slug = models.SlugField(blank=True, unique=True)

观点PY

def dashboard_view(request, slug):
    userprofile = Profile.objects.get(slug=slug)
    user_viewed_object = userprofile.objectviewed_set.first()
    user_blog = userprofile.userblog_set.first()
    user_watchlist = userprofile.watchlist_set.first()

    context = {
        'user_profile_detail': userprofile,
        'user_blog': user_blog,
        'user_watchlist': user_watchlist,
        'user_viewed_object': user_viewed_object,
        'page_name': "Dashboard"
    }
    print(userprofile.slug)
    return render(request, 'userprofile/home.html', context)


def profile_view(request, slug):
    user_profile_detail = Profile.objects.get(slug=slug)
    if request.method == 'POST':
        form = ProfileForm(request.POST or None, request.FILES, instance=user_profile_detail)
        if form.is_valid():
            print(form)
            form.save(commit=False)
            user_profile_detail = request.user
            form.save()
            return HttpResponseRedirect('/')

    context = {
        'profile': user_profile_detail,
        'page_name': 'Profile',
        'form': form
    }
    return render(request, 'userprofile/profile.html', context)

URLS.PY

app_name = 'userprofile'

urlpatterns = [
    path('<slug:slug>', dashboard_view, name="dashboard"),
    path('<slug:slug>/user_profile/', profile_view, name='profile'),
    path('<slug:slug>/watchlist/', watchlist_view, name='watchlist'),
    path('<slug:slug>/blog/', userblog_view, name="blog"),
    path('<slug:slug>/history/', user_history, name="history"),
]

错误消息:

NoReverseMatch at /profile/jacklit/user_profile/
Reverse for 'dashboard' with arguments '('',)' not found. 1 pattern(s) tried: ['profile\\/(?P<slug>[-a-zA-Z0-9_]+)$']

profile.html

{% extends 'userprofile/dashboard.html' %}
{% load crispy_forms_tags %}

{% block content %}

<div class="container-fluid mt-5">

    {% include 'userprofile/dashboard_head.html' %}

    {% include 'userprofile/large_center_modal.html' %}

    <div class="card">
        <h2 class="my-3 mx-3"><i class="fa fa-user mr-3" aria-hidden="true"></i>My Profile</h2>
        <hr >
        <div class="row my-5 mx-1">
            <div class="col-3 text-center pl-3">
                {% if profile.profile_picture %}
                <img src="{{ profile.profile_picture.url }}" alt=""
                    class="img-fluid z-depth-1-half rounded-circle">
                {% endif %}
                <div style="height: 10px"></div>
                <p class="title mb-0">{{ profile.first_name }} {{ profile.last_name }}</p><br>
                <p class="title mb-0" style="font-size: 13px">{{ profile.role }}</p><br>
                <p class="title mb-0"><b>Joined: </b>{{ profile.joined }}</p><br><br>

                {% if request.user == profile.user %}
                    <a href="#" data-toggle="modal" data-target="#centralModalLGInfoDemo">Edit Profile</a>
                {% endif %}

            </div>

            <div class="col-9">

                <h3><u>Biography</u></h3>
                <h6>{{ profile.biography }}</h6>
                <hr>
                <h6><i class="fa fa-envelope mr-3" aria-hidden="true"></i>Email Address: {{ profile.email }}</h6>
                <h6><i class="fa fa-phone mr-3" aria-hidden="true"></i>Office Number:{{ profile.office_number }}</h6>
                <h6><i class="fa fa-mobile-phone mr-3" aria-hidden="true"></i>Phone Number: {{ profile.phone_number }}</h6>
                <br><br>

                <h3><u>Contact Information</u></h3>
                <h6>Email: {{ profile.email }}</h6>
                <h6>Office: {{ profile.office_number }}</h6>
                <h6>Phone: {{ profile.phone_number }}</h6><br><br>

                <h3><u>Social Medias</u></h3>
                <a href="{{ profile.google_link }}"><h6><i class="fa fa-google-plus mr-3"></i>Google Plus:</h6></a>
                <a href="{{ profile.facebook_link }}"> <h6><i class="fa fa-facebook mr-3"></i>Facebook: </h6></a>
                <a href="{{ profile.twitter_link }}"><h6><i class="fa fa-twitter mr-3"></i>Twitter: </h6></a>
                <a href="{{ profile.linkedin_link }}"><h6><i class="fa fa-linkedin mr-3"></i>LinkedIn: </h6></a>

            </div>
        </div>
    </div>
</div>

{% endblock %}

Large_center_Modal.html

<form action="{% url 'userprofile:profile' user_profile_detail.slug %}" enctype="multipart/form-data" method="post">
    {% csrf_token %}
    <div class="modal fade" id="centralModalLGInfoDemo" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-lg modal-notify modal-info" role="document">
            <!--Content-->
            <div class="modal-content">
                <!--Header-->
                <div class="modal-header">
                    <p class="heading lead">Update my Profie</p>

                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true" class="white-text">&times;</span>
                    </button>
                </div>

                <!--Body-->
                <div class="modal-body">
                    <div class="">
                        <div class="col-10">{{ form | crispy }}</div>
                    </div>


                </div>

                <!--Footer-->
                <div class="modal-footer">
                    <button type="submit" class="btn btn-outline-info waves-effect">Update</button>
                </div>
            </div>
            <!--/.Content-->
        </div>
    </div>
</form>

【问题讨论】:

    标签: django django-views


    【解决方案1】:

    您的上下文字典有profile 键:'profile': user_profile_detail,。所以在模板中你应该使用profile 而不是user_profile_detail

    "{% url 'userprofile:dashboard' profile.slug %}" 
    

    【讨论】:

    • 对不起,我忘了补充一点,我有另一个视图可以呈现 sidenav 使用的 html 标签
    • @user8795870 好的。你能添加profile.html 模板吗?看起来错误在那里。
    • @user8795870 你的profile.html 扩展并包含许多其他模板。检查此模板是否包含类似"{% url 'userprofile:dashboard' profile.slug %}" 的内容。你需要根据我的回答来修复它。
    • 我意识到我的所有模板都共享相同的“navbar.html”的问题,并且所有模板都使用不同的名称作为上下文来获取对象,因此出现了问题。
    • 问题已解决!!非常感谢你有任何关于我可以组织我的 html 模板的提示,这样这样的事情就不会再发生了吗?在您提到包含的模板之前,很难检测到错误
    猜你喜欢
    • 2013-09-26
    • 2013-01-19
    • 2015-07-18
    • 2014-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-14
    • 2016-06-04
    相关资源
    最近更新 更多