简体   繁体   中英

Django unexpected characters in URL

I am making a blog and trying to make pagination operations on post listing page. When I run my app, my URL contains unexpected characters. For example ;

http://127.0.0.1:8000/blog/%5E$

I couldn't understand why %5E$ is there.

Here my urls.py (this is in blogapp):

from django.urls import path,include
from django.contrib import admin

from . import views

urlpatterns = [
    path(r'^$',views.getPosts,name="bloghome"),
    path(r'^(?P<selected_page>\d+)/?$',views.getPosts,name="bloghome"),    
    path('<slug>',views.postDetailPage,name="post_detail")
]

getPost function in views.py

def getPosts(request,selected_page=1):
#    latest_post = Posts.objects.get(id=1)
    posts = Posts.objects.all().order_by('-pub_date')
    pages = Paginator(posts,5) #Show 5 post per page
    try:
        returned_page = pages.page(selected_page)
    except EmptyPage:
        returned_page = pages.page(pages.num_pages)
    #content = pages.page(selected_page)    
    return render(request,'blog.html',{'page':returned_page,
                                       'posts':returned_page.object_list
                                        })

And finally, this bloglist page is being entered from homepage with <a> tag. Here it's one line of code:

<a href="{% url 'bloghome'%}">Blog</a> 

Based upon https://docs.djangoproject.com/en/dev/ref/urls/#django.urls.path , you need to use re_path() instead of path() , as it is interpreting ^$ literally, per zvadym's prior comment. This is new in Django 2.0, so it depends on your version.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM