0

Whenever I click the button this error showup

I have no idea why is this happening.

error

NoReverseMatch at /admin-chat-list/
'admin-chat' is not a registered namespace

html file

<form mthod="POST" action="{% url 'admin-chat:{{data.email}}' %}">
     <input type="submit" value="CHAT">
</form>

urls.py

path('admin-chat/<str:merchant_email>/', views.admin_chat, name='admin-chat'),

views.py

def admin_chat(request, merchant_email):
    print(merchant_email)

    if request.method == 'POST':
        msg = request.POST['message']
        Message.objects.create(sender=request.user,
                               receiver=merchant_email, message=msg)
        return redirect('admin-chat')

    return render(request, 'dashboard/admin/chat.html')
Sagar Yadav
  • 109
  • 2
  • 10
  • 1
    Does this answer your question? [How to add url parameters to Django template url tag?](https://stackoverflow.com/questions/25345392/how-to-add-url-parameters-to-django-template-url-tag) – JPG Sep 23 '20 at 15:28

1 Answers1

1

The format of your django url tag is incorrect.

You are not using a namespace in your urls.py file, but you are naming one in your url tag:

<form method="POST" action="{% url 'admin-chat:{{data.email}}' %}">

In a django url tag, the text before the colon ':' is used specifically (and only) to name a namespace. Your tag, as currently written, expects your urls.py file to look like this:

app_name = 'admin-chat'
url_patterns = [
path('admin-chat/<str:merchant_email>/', views.admin_chat, name='admin-chat'),
]

Since you are not using a namespace like above, you cannot reference a namespace in your url tag. I see that you've named your URL pattern 'admin-chat', so it seems you are simply confused about the proper syntax of the django url tag. When you put 'admin-chat' before : inside the tag, django does not read that as your named URL, it reads it as a namespace. (Note: the above example code is meant only to show what your current tag is looking for in your urls.py; in actuality, you would never want to have a namespace that is identical to a named url, as seen above).

A django url tag has the format: {% url 'some-url-name' optional_arguments %}. Therefore, you can simply provide your named url pattern, admin-chat, in quotes, and after that, add the additional argument(s) you are passing to the url:

<form method="POST" action="{% url 'admin-chat' data.email  %}">

This is the correct format for a django url tag with an (optional) argument included. Note that when adding the argument, you do not need to nest a django variable tag (double curly braces) inside a django template tag, as you've done in your original code.

m.arthur
  • 299
  • 2
  • 8