Django

时间:2019-01-27 10:53:36

标签: django django-channels

我可能在这里丢失了一些东西,但是我有一个连接页面,在该页面中,登录用户可以通过单击每个用户配置文件上的Chat?链接来与其他用户聊天。

聊天功能是使用Django渠道构建的,可以通过url模式进行访问

site/messages/username

其中用户名是other_user与之聊天的current_user

我目前的chat?链接为

<a class='btn btn-light' href="{% url 'thread' username %}" id="chat">Chat?</a>

但是会引发错误 NoReverseMatch Reverse for 'thread' not found. 'thread' is not a valid view function or pattern name.

我尝试按照文档操作,并将用户名作为arg添加/删除,但我一直遇到相同的错误。

该聊天应用包含在settings.py中,而网址包含在admin.py中。

连接和聊天是独立的应用。

下面是相关代码。谢谢您的时间和帮助!

聊天/urls.py

from django.urls import path, re_path
from .views import ThreadView, InboxView

app_name = 'chat'
urlpatterns = [
    path("", InboxView.as_view()),
    re_path(r"^(?P<username>[\w.@+-]+)", ThreadView.as_view(), name='thread'),
]

聊天/views.py

class ThreadView(LoginRequiredMixin, FormMixin, DetailView):
    template_name = 'chat/thread.html'
    form_class = ComposeForm
    success_url = './'

    def get_queryset(self):
        return Thread.objects.by_user(self.request.user)

    def get_object(self):
        other_username  = self.kwargs.get("username")
        obj, created    = Thread.objects.get_or_new(self.request.user, other_username)
        if obj == None:
            raise Http404
        return obj

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['form'] = self.get_form()
        return context

回溯

Internal Server Error: /connect/
Traceback (most recent call last):
  File "/Users/xx/xx/project/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/Users/xx/xx/project/lib/python3.7/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/xx/xx/project/lib/python3.7/site-packages/django/core/handlers/base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/xx/xx/project/lib/python3.7/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/Users/xx/xx/project/src/project/connect/views.py", line 15, in connect_home_view
    return render(request, 'connect/home.html', context)
  File "/Users/xx/xx/project/lib/python3.7/site-packages/django/shortcuts.py", line 36, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "/Users/xx/xx/project/lib/python3.7/site-packages/django/template/loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "/Users/xx/xx/project/lib/python3.7/site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "/Users/xx/xx/project/lib/python3.7/site-packages/django/template/base.py", line 171, in render
    return self._render(context)
  File "/Users/xx/xx/project/lib/python3.7/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "/Users/xx/xx/project/lib/python3.7/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/Users/xx/xx/project/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/Users/xx/xx/project/lib/python3.7/site-packages/django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "/Users/xx/xx/project/lib/python3.7/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "/Users/xx/xx/project/lib/python3.7/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/Users/xx/xx/project/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/Users/xx/xx/project/lib/python3.7/site-packages/django/template/loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "/Users/xx/xx/project/lib/python3.7/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/Users/xx/xx/project/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/Users/xx/xx/project/lib/python3.7/site-packages/django/template/defaulttags.py", line 209, in render
    nodelist.append(node.render_annotated(context))
  File "/Users/xx/xx/project/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/Users/xx/xx/project/lib/python3.7/site-packages/django/template/defaulttags.py", line 442, in render
    url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
  File "/Users/xx/xx/project/lib/python3.7/site-packages/django/urls/base.py", line 90, in reverse
    return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
  File "/Users/xx/xx/project/lib/python3.7/site-packages/django/urls/resolvers.py", line 622, in _reverse_with_prefix
    raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'thread' with arguments '('',)' not found. 1 pattern(s) tried: ['messages/(?P<username>[\\w.@+-]+)']
[2019/01/27 12:36:43] HTTP GET /connect/ 500 [0.10, 127.0.0.1:50815]

2 个答案:

答案 0 :(得分:1)

您正在使用app_name = 'chat'。这使您的应用程序级别的URL在应用程序名称空间下可用。查看文档:{​​{3}}

这应该有效:

{% url 'chat:thread' username %}

答案 1 :(得分:1)

由于您已在网址中为应用命名,因此 app_name ='聊天' 您的网址必须具有指定的名称

<a class='btn btn-light' href="{% url 'chat:thread' username %}" id="chat">Chat?</a>

这在编程中称为命名空间