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

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 :

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:

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

my post function in views.py in posts app:

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

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,

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.

1 Like

Thank you man
I edited it…

Do you have any Idea for this problem?! :frowning:

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

1 Like

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

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

and this my post.html template:

{% 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

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:

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

1 Like

this line in the main template

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

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’?)

1 Like

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…

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.)

1 Like

Do you mean this?

<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>
1 Like

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.

1 Like

root urls.py

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

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"),
]

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:

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

(See 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.)

1 Like

Hi Ken :white_heart:
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

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…