# persian slug in django urls in page start with persian slug address

**URL:** <https://forum.djangoproject.com/t/persian-slug-in-django-urls-in-page-start-with-persian-slug-address/29998>\
**Category:** Mystery Errors\
**Created:** [April 9, 2024, 1:09pm UTC](https://forum.djangoproject.com/t/persian-slug-in-django-urls-in-page-start-with-persian-slug-address/29998 "2024-04-09T13:09:59Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![christnn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/christnn/32/20492_2.png) [@christnn](https://forum.djangoproject.com/u/christnn)\
**Post date:** [April 9, 2024, 1:09pm UTC](https://forum.djangoproject.com/t/persian-slug-in-django-urls-in-page-start-with-persian-slug-address/29998/1 "2024-04-09T13:09:59Z")

</div>

when I use persian slug to get my post details I have a special problem…

in the pages with persian slug urls in the page start with corrent page address like this :

in the page : `example.com/سلام-دنیا` when I click on the signUp button istead of this url:` example.com/signUp` is `example.com/سلام-دنیا/signUp`

**with english slug everything ok and working great**

**and whole this proccess is ok in the my localhost but on the cpanel host This problem occurs.**

my urls.py posts app :

```auto
from django import urls
from django.urls import path , re_path
from . import views

urlpatterns = [
    
    path('', views.posts , name='posts'),
    path('new_post/', views.new_post , name='new_post'),
   re_path(r'(?P<slug>[^/]+)/?

```

in my template:

```auto
<a class="btn btn-sm btn-outline-secondary" href="{%url "login"%}">عضویت/ورود</a>

```

my post function in views.py in posts app:

```auto
import os
from uuid import uuid4
from django.shortcuts import render , redirect
from django.contrib import messages
from config import settings
from posts.forms import CommentForm, PostCreatorFrom
from .decorators import user_is_superuser
from posts.utils import paginatePosts, searchPosts
from .models import Category, Comment, Post
from django.core.paginator import Paginator
from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse
from pathlib import Path
from urllib.parse import unquote
from django.views.decorators.http import require_POST
from django.utils.text import slugify
from django.utils.encoding import uri_to_iri

def post(request, slug):
    slug = uri_to_iri(slug)
    postObj = Post.objects.get(slug=slug)
    form = CommentForm()
    category = postObj.category
    related_posts = Post.objects.filter(category=category).exclude(slug=slug)[:3]
    

    return render(request, 'posts/post.html', {'post': postObj, 'form': form , 'related_posts':related_posts})

```

and my model

```auto
class Post(models.Model):
    title = models.CharField(max_length=500 , verbose_name="عنوان")
    slug = models.SlugField(unique=True , verbose_name="آدرس" , allow_unicode=True , db_collation='utf8_persian_ci')

```

best regards,

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 9, 2024, 1:12pm UTC](https://forum.djangoproject.com/t/persian-slug-in-django-urls-in-page-start-with-persian-slug-address/29998/2 "2024-04-09T13:12:34Z")

</div>

Welcome @christnn !

I think your post got a bit mangled here. You may wish to edit it to include the actual code and template being referenced instead of these “DISCOURSE\_PLACEHOLDER” elements.

---

<div class="post-metadata">

**Author:** ![christnn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/christnn/32/20492_2.png) [@christnn](https://forum.djangoproject.com/u/christnn)\
**Post date:** [April 9, 2024, 1:16pm UTC](https://forum.djangoproject.com/t/persian-slug-in-django-urls-in-page-start-with-persian-slug-address/29998/3 "2024-04-09T13:16:23Z")

</div>

Thank you man  
I edited it…

Do you have any Idea for this problem?! ☹

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 9, 2024, 1:17pm UTC](https://forum.djangoproject.com/t/persian-slug-in-django-urls-in-page-start-with-persian-slug-address/29998/4 "2024-04-09T13:17:40Z")

</div>

> [@christnn](#):
>
> in the pages with persian slug urls in the page start with corrent page address like this :
> 
> in the page : `example.com/سلام-دنیا` when I click on the signUp button istead of this url:` example.com/signUp` is `example.com/سلام-دنیا/signUp`

What does the template look like for this page?  
(The page where this `signUp` button is being rendered with the incorrect url.)

---

<div class="post-metadata">

**Author:** ![christnn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/christnn/32/20492_2.png) [@christnn](https://forum.djangoproject.com/u/christnn)\
**Post date:** [April 9, 2024, 1:28pm UTC](https://forum.djangoproject.com/t/persian-slug-in-django-urls-in-page-start-with-persian-slug-address/29998/5 "2024-04-09T13:28:33Z")

</div>

In my main template that’s other templates extend it:

```auto
        <a class="btn btn-sm btn-outline-secondary" href="{%url 'login'%}">عضویت/ورود</a>

```

and this my post.html template:

```auto
{% extends 'main.html' %}
{% load static %}
{% load jalali_tags %}
{% block content %}
<head>
  <link rel="stylesheet" href="{% static 'styles/app.css' %}" />
  <link rel="stylesheet" type="text/css" href="{% static 'styles/prism.css'%}">

</head>

  <div class="nav-scroller py-1 mb-3 border-bottom">
    <nav class="nav nav-underline justify-content-between">
      {%for category in categories%}
      <a class="nav-item nav-link link-body-emphasis active" href="#">{{category}}</a>
      {%endfor%}
    </nav>
  </div>
</div>

<main class="container">
  <div class="row g-5">
    <div class="col-md-8">
      <article class="blog-post">
        <h2 class="display-5 link-body-emphasis mb-1">{{post.title}}</h2>
        <p class="blog-post-meta">{{post.created|to_jalali}}

          {%if post.user == request.user%}
          | 
          <strong><a href="{%url "edit_post" post.id%}">ویرایش نوشته</a></strong>
          {%endif%}
        </p>
        <p>{{post.description}}</p>
        <hr>
        {{post.content|safe}}
      </article>

      <nav class="blog-pagination" aria-label="Pagination">
        <a class="btn btn-outline-primary rounded-pill" href="#">برو به اول صفحه</a>
      </nav>

    </div>

    <div class="col-md-4">
      <div class="position-sticky" style="top: 2rem;">
        <div class="p-4 mb-3 bg-body-tertiary rounded">
          <h4 class="">درباره 
            {{post.user.first_name}}
            {{post.user.last_name}}
          </h4>
          <p class="mb-0">
          {{post.user.biography}}
        </div>

        <div>
          <h4 class="">مطالب مرتبط</h4>
          <ul class="list-unstyled">
            {%for related_post in related_posts%}
            <li>
              <a class="d-flex flex-column flex-lg-row gap-3 align-items-start align-items-lg-center py-3 link-body-emphasis text-decoration-none border-top" href="{%url "post" related_post.slug%}">
                <img class="object-fit-cover" width="100%" height="96" src="{{related_post.imageURL}}" alt="{{related_post.title}}">
                
                <div class="col-lg-8">
                  <h6 class="mb-0">{{related_post.title}}</h6>
                  <small class="text-body-secondary">{{related_post.created}}</small>
                </div>
              </a>
            </li>
            {%endfor%}
          </ul>
        </div>
      </div>
    </div>
  </div>

</main>
{{ form.media }}
<script src="{% static 'js/main.js' %}"></script>
<script src="{% static 'js/prism.js' %}"></script>
{% endblock %}

```

and all of this urls that you see when I use persian slug start with current page url!!

**404 page rendered instead correct page**

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 9, 2024, 1:42pm UTC](https://forum.djangoproject.com/t/persian-slug-in-django-urls-in-page-start-with-persian-slug-address/29998/6 "2024-04-09T13:42:38Z")

</div>

I’m sorry, I’m not following you here, and I don’t think you answered my question.

You have a template somewhere that is rendering this text as a link:

> [@christnn](#):
>
> [example.com/سلام-دنیا/signUp](http://example.com/%D8%B3%D9%84%D8%A7%D9%85-%D8%AF%D9%86%DB%8C%D8%A7/signUp)

What line in what template is rendering _this_ as a link?

---

<div class="post-metadata">

**Author:** ![christnn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/christnn/32/20492_2.png) [@christnn](https://forum.djangoproject.com/u/christnn)\
**Post date:** [April 9, 2024, 1:46pm UTC](https://forum.djangoproject.com/t/persian-slug-in-django-urls-in-page-start-with-persian-slug-address/29998/7 "2024-04-09T13:46:30Z")

</div>

this line in the main template

```auto
        <a class="btn btn-sm btn-outline-secondary" href="{%url 'login'%}">عضویت/ورود</a>

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 9, 2024, 1:48pm UTC](https://forum.djangoproject.com/t/persian-slug-in-django-urls-in-page-start-with-persian-slug-address/29998/8 "2024-04-09T13:48:07Z")

</div>

That looks like that’s for a “login” link, not a “signUp” link.

(What does your urls.py definition look like for the url named ‘login’?)

---

<div class="post-metadata">

**Author:** ![christnn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/christnn/32/20492_2.png) [@christnn](https://forum.djangoproject.com/u/christnn)\
**Post date:** [April 9, 2024, 1:51pm UTC](https://forum.djangoproject.com/t/persian-slug-in-django-urls-in-page-start-with-persian-slug-address/29998/9 "2024-04-09T13:51:23Z")

</div>

No It’s correct

`path('signUp/', views.loginUser, name="login"),`

this link is just a example I sayed …  
all of the links in the page that I getting by persian slug url as the same … and have same problem…

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 9, 2024, 1:57pm UTC](https://forum.djangoproject.com/t/persian-slug-in-django-urls-in-page-start-with-persian-slug-address/29998/10 "2024-04-09T13:57:13Z")

</div>

> [@christnn](#):
>
> this link is just a example I sayed …

I understand. If you would prefer using a different example, we can. But I’m focusing on this one because this is the one you selected as the example.

Please show me the actual html for the url as it exists in the browser for this link. (You can use your browser’s developer tools to examine it and copy it for here.)

---

<div class="post-metadata">

**Author:** ![christnn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/christnn/32/20492_2.png) [@christnn](https://forum.djangoproject.com/u/christnn)\
**Post date:** [April 9, 2024, 2:01pm UTC](https://forum.djangoproject.com/t/persian-slug-in-django-urls-in-page-start-with-persian-slug-address/29998/11 "2024-04-09T14:01:17Z")

</div>

Do you mean this?

```auto
<a class="btn btn-sm btn-outline-secondary" href="/blog/%D9%85%D8%AB%D8%A7%D9%84%DB%8C-%D8%A8%D8%B1%D8%A7%DB%8C-%D9%85%D8%A7%D8%AA%D8%B1%DB%8C%D8%B3-%D8%AF%D8%B1%D9%87%D9%85-%D8%B1%DB%8C%D8%AE%D8%AA%DA%AF%DB%8C-%D8%AF%D8%B1-%DB%8C%D8%A7%D8%AF%DA%AF%DB%8C%D8%B1%DB%8C-%D9%85%D8%A7%D8%B4%DB%8C%D9%86-%D8%A8%D8%A7/signUp/">عضویت/ورود</a>

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 9, 2024, 2:10pm UTC](https://forum.djangoproject.com/t/persian-slug-in-django-urls-in-page-start-with-persian-slug-address/29998/12 "2024-04-09T14:10:26Z")

</div>

Your urls.py file in the original post appears to have been truncated.

Please post the complete urls.py file for your app, along with your root urls.py file showing the “blog” entry.

---

<div class="post-metadata">

**Author:** ![christnn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/christnn/32/20492_2.png) [@christnn](https://forum.djangoproject.com/u/christnn)\
**Post date:** [April 9, 2024, 2:13pm UTC](https://forum.djangoproject.com/t/persian-slug-in-django-urls-in-page-start-with-persian-slug-address/29998/13 "2024-04-09T14:13:21Z")

</div>

root urls.py

```auto
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('posts.urls')),
    path('accounts', include('accounts.urls')),
    path('projects/', include('projects.urls')),
    path('tinymce/', include('tinymce.urls') ),
    path('reset_password/', auth_views.PasswordResetView.as_view(template_name="reset_password.html"),
         name="reset_password"),

    path('reset_password_sent/', auth_views.PasswordResetDoneView.as_view(template_name="reset_password_sent.html"),
         name="password_reset_done"),

    path('reset/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(template_name="reset.html"),
         name="password_reset_confirm"),

    path('reset_password_complete/', auth_views.PasswordResetCompleteView.as_view(template_name="reset_password_complete.html"),
         name="password_reset_complete"),

    re_path(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),

  re_path(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),

]

```

and posts app urls.py

```auto
from django import urls
from django.urls import path , re_path
from . import views

urlpatterns = [
    
    path('', views.posts , name='posts'),
    path('new_post/', views.new_post , name='new_post'),
    path('edit-post/<str:pk>/', views.edit_post , name='edit_post'),
    path('<post>/upload_image/', views.upload_image , name='upload_image'),
    path('add-to-favorites/<int:post_id>/', views.add_to_favorites, name='add_to_favorites'),
    path('delete-from-favorites/<int:post_id>/', views.delete_from_favorites, name='delete_from_favorites'),
   re_path(r'blog/(?P<slug>[^/]+)/?$', views.post, name="post"),
]

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 9, 2024, 2:31pm UTC](https://forum.djangoproject.com/t/persian-slug-in-django-urls-in-page-start-with-persian-slug-address/29998/14 "2024-04-09T14:31:46Z")

</div>

What appears to be happening is that your reference to the `login` url name is actually reversing the url named `post`.

Is the `accounts.urls` that you are referencing here something that _you_ have defined or are you referencing the system-provided `accounts.urls` file?

If this is your file, please post it.

Beyond that, there are a couple things I’ve noticed:

> [@christnn](#):
>
> ```auto
> path('accounts', include('accounts.urls')),
> 
> ```

should be this:  
` path('accounts/', include('accounts.urls')),`

(See [Using the views](https://docs.djangoproject.com/en/5.0/topics/auth/default/#using-the-views))

Second, I recommend putting url definitions like this:  
` path('', include('posts.urls')),`  
at the end of the urlpatterns list to help avoid causing an accidental override of a specified url.  
(Actually, I always recommend _against_ using this “blank” path with an “include”. it tends to eventually create some type of difficult-to-find error. I would strongly suggest that you use “blog/” as the common prefix here, such that your app urls end up looking something like “blog/” or “blog/new\_post/”, etc.)

Try making these two corrections first. I’m doubtful that they will actually solve the problem, but they should be corrected, and may reveal a different issue.

Another item you’ll want to check is to verify that you don’t have _any_ two url definitions with the same name. (Preventing that situation is one of the reasons for using a namespace for your urls.)

---

<div class="post-metadata">

**Author:** ![christnn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/christnn/32/20492_2.png) [@christnn](https://forum.djangoproject.com/u/christnn)\
**Post date:** [April 10, 2024, 7:42am UTC](https://forum.djangoproject.com/t/persian-slug-in-django-urls-in-page-start-with-persian-slug-address/29998/15 "2024-04-10T07:42:13Z")

</div>

Hi Ken 🤍  
Thank you for your time

I did the two things you mentioned , And unfortunately it was unsuccessful, even I try without blank path and … Again it was without result…!!

yes accounts is a app in my project and urls.py:

There was a mistake that was just a writing mistake and I solved it, I had mistakenly written login as signup, which I corrected in this file. I apologize if it confused you

```auto
from django.urls import include, path
from . import views
urlpatterns = [   
    path('', views.Profile, name='profile'),
    path('profiles/', views.Profile, name='profiles'),
    path('login/', views.loginUser, name="login"),
    path('logout/', views.logoutUser, name="logout"),
    path('register/', views.registerUser, name="register"),
    path('support/', views.support, name="support"),
    path('editProfile/', views.editProfile, name="editProfile"),
    path('editPassword/', views.editPassword, name="editPassword"),
    path('sendTicket/', views.sendTicket, name="sendTicket"),
    path('tickets/', views.tickets, name="tickets"),
    path('ticket/<str:pk>/', views.ticket, name="ticket"),
    path('ticketManager/', views.ticketManager, name="ticketManager"),
    path('ticketOpen/<str:pk>/', views.ticketOpen, name="ticketOpen"),
    path('ticketClose/<str:pk>/', views.ticketClose, name="ticketClose"),
    path('phoneVerify/', views.phoneVerify, name="phoneVerify"),
    path('aboutUs/', views.aboutUs, name="aboutUs"),
    path('inbox/', views.inbox, name="inbox"),

]

```

One thing I want to say, maybe it will help to keep you more informed. I forgot to say yesterday that I had the exact same problem with static files.  
That is,just in cpanel host on the pages whose address was persian slug, the address of my static files was also facing the same problem, and the address of the current page, I solved the problem by changing the configuration of static files in settings.py and calling python manage.py collectstatics, and now static files are loaded on these pages without any problem.

And again, I remind that everything works correctly in localhost…

---

<div class="post-metadata">

**Author:** ![ahmadrezanavaie](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ahmadrezanavaie/32/25241_2.png) [@ahmadrezanavaie](https://forum.djangoproject.com/u/ahmadrezanavaie)\
**Post date:** [November 18, 2024, 1:59pm UTC](https://forum.djangoproject.com/t/persian-slug-in-django-urls-in-page-start-with-persian-slug-address/29998/16 "2024-11-18T13:59:27Z")

</div>

Finally, after a lot of different tests, I realized that the problem was with Django. I had version 5.1.3 installed, and this issue persisted without any solution. However, after uninstalling Django and installing version 4.2, the problem got resolved. This bug should be reported to Django for them to fix it.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 18, 2024, 2:02pm UTC](https://forum.djangoproject.com/t/persian-slug-in-django-urls-in-page-start-with-persian-slug-address/29998/17 "2024-11-18T14:02:48Z")

</div>

Welcome @ahmadrezanavaie !

Just a side note to your comment here: Django is an OpenSource Community-driven project. There is no “them” to fix anything, only “us”.

The fastest way for you to see this resolved _may_ be for you to identify what changed between 4.2 and 5.1 that is now causing this to fail, and to file the appropriate ticket. It would be even better if you identifed what the solution would be.

---

<div class="post-metadata">

**Author:** ![karimeh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/karimeh/32/27654_2.png) [@karimeh](https://forum.djangoproject.com/u/karimeh)\
**Post date:** [March 22, 2025, 1:46am UTC](https://forum.djangoproject.com/t/persian-slug-in-django-urls-in-page-start-with-persian-slug-address/29998/18 "2025-03-22T01:46:24Z")

</div>

There is a problem with Persian slug in version 5.0a1.  
This problem does not exist in version 4.2.2.  
It seems that Django cannot generate the base\_url that should link to the home page in internal post pages that use re\_path.

---

<div class="post-metadata">

**Author:** ![ahmadrezanavaie](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ahmadrezanavaie/32/25241_2.png) [@ahmadrezanavaie](https://forum.djangoproject.com/u/ahmadrezanavaie)\
**Post date:** [May 19, 2025, 9:02am UTC](https://forum.djangoproject.com/t/persian-slug-in-django-urls-in-page-start-with-persian-slug-address/29998/20 "2025-05-19T09:02:25Z")

</div>

Thank you so much for your advice and encouragement, Ken 🤍!

Following your suggestion, I did a deeper investigation and found the specific cause of this issue. The root of the problem is in the change to how `script_name` is calculated in `django/core/handlers/wsgi.py` in Django 5. In this latest version, the line:

```auto

script_name = script_url.removesuffix(path_info)

```

was introduced. The problem is that `removesuffix()` doesn’t work properly when `script_url` and `path_info` have different encodings (for example, when URLs contain non-ASCII slugs like Persian or other Unicode characters and the paths are URL-encoded). As a result, Django fails to calculate the correct absolute URL, breaking many cases that worked in version 4.2.

To demonstrate, here are some logs from accessing a URL with a Persian slug:

```auto

-----script_url-----: b'/\xd9\x85\xd8\xad\xd8\xb5\xd9\x88\xd9\x84-\xd8\xa7\xd9\x88\xd9\x84\xdb\x8c/'
-----path_info-----: b'/%D9%85%D8%AD%D8%AD%D8%B5%D9%88%D9%84-%D8%A7%D9%88%D9%84%DB%8C/'
-----script_name_removesuffix-----: b'/\xd9\x85\xd8\xad\xd8\xb5\xd9\x88\xd9\x84-\xd8\xa7\xd9\x88\xd9\x84\xdb\x8ی/'
-----script_name_slice-----: b'' #correct

```

As you can see, even though `script_url` and `path_info` refer to the same path, their encodings are different. `removesuffix()` doesn’t strip the suffix, creating the bug.

**Easiest Solution:**  
By changing that line to use string slicing by length instead, the issue goes away—even if encodings differ:

```auto

script_name = script_url[: -len(path_info)] if path_info else script_url

```

I am preparing a ticket (and hopefully a pull request) so this can be addressed in Django itself. I hope this is the kind of contribution the community wants. Thanks again for pointing me in the right direction!
