NoReverseMatch错误,即使视图和URL路径已经存在

时间:2019-03-27 23:05:58

标签: python django django-templates django-views django-urls

我仍在学习使用django进行编程的绳索,当我尝试在localhost上打开我的网站时,我收到一条错误消息,该错误消息无助于指示我正确的方向。 这是我从名为base.html的文件中获取的错误消息:

NoReverseMatch at /
Reverse for 'main-createchannel' not found. 'main-createchannel' is not a valid view function or pattern name.

base.html调用网址的部分

<div class="collapse navbar-collapse" id="navbarToggle">
    <div class="navbar-nav mr-auto">
        <a class="nav-item nav-link" href="{% url 'main:main-about' %}">About</a>

        <a class="nav-item nav-link" href="{% url 'main:main-channelsettings' %}">Channel Settings</a>
        <a class="nav-item nav-link" href="{% url 'main:main-channelinfo' %}">Channel Information</a>
        <a class="nav-item nav-link" href="{% url 'main:main-createchannel' %}">Create Channel</a>
        <a class="nav-item nav-link" href="{% url 'main:main-findchannel' %}">Find Channel</a>

        <a class="nav-item nav-link" href="{% url 'main:main-ticketrequest' %}">Submit a Ticket</a>
    </div>
    <!-- Navbar Right Side -->
    <div class="navbar-nav">
        <a class="nav-item nav-link" href="{% url 'main:main-userprofile' %}">Login</a>
        <a class="nav-item nav-link" href="{% url 'main:users-register' %}">Register</a>
        {% if user.is_authenticated %}
            <a class="nav-item nav-link" href="{% url 'main:main-userprofile' %}">Profile</a>
        {% endif %}
    </div>
</div>

views.py

def createchannelpage(request):
    if request.method == 'POST':
        form = CreateChannelForm(request.POST)
        if form.is_valid():
            form.save()
            channel_room_name = form.cleaned_data.get('channel_room_name')
            #messages.success(request, f'{channel_room_name} was created!')
            return redirect('/')
    else:
        form = CreateChannelForm()
    return render(request, 'main/createChannel.html', {
        'form': form,
        'title': "Create a Channel"
    })

最后是urls.py

from . import views
from users import views as users_views

app_name = 'main'

urlpatterns = [
    path('', views.homepage, name='main-home'),
    path('register/', users_views.register, name='users-register'),
    path('about/', views.aboutpage, name='main-about'),
    path('createchannel/', views.createchannelpage, name='main-createchannel'),
    path('findchannel/', views.findchannelpage, name='main-findchannel'),
    path('channelinfo/', views.channelinfopage, name='main-channelinfo'),
    path('channelsettings/', views.channelsettingspage, name='main-channelsettings'),
    path('userprofile/', views.userprofilepage, name='main-userprofile'),
    path('ticketrequest',views.ticketrequestpage, name='main-ticketrequest'),
    re_path(r'^(?P<channel_room_name>[^/]+)/$', views.channelinfopage, name='channel'),
]

当我尝试使用{%url views.createchannelpage%}调用页面时,此操作无效。但是都没有按名称调用url,例如{%url'main-createchannel'%}。

我觉得我已经尝试了所有可以在网上找到的东西来帮助我。我错过某处只是简单的事情吗?谢谢!

0 个答案:

没有答案