Hi so far i tried doing it, but i can only access it by using id value.
so http://localhost:8000/1/ and not http://localhost:8000/1/)username/
(How can i achieve that ?
This is all i have tried: Please Help ![:slight_smile: :slight_smile:](https://emoji.discourse-cdn.com/twitter/slight_smile.png?v=9)
urls.py
path("<str:author>/", Profile, name = "Profile"),
Views.py:
def Profile(request,author):
A = Post.objects.filter(author = author)
return render(request,"blog/profile.html",{"A":A})
My html Profile Page:
{% extends "blog/base.html" %}
{% block content %}
{% for S in A %}
<ul>
<li><b>{{S.title}}</b></li>
</ul>
{% endfor %}
{% endblock content %}
(Note: Iâm assuming the close paren in the original text is a typo and have removed it here.)
The issue here is that the url youâre using here contains both the id (1/
) and the username, but your url is only accepting username.
You would need to either remove the 1/
from the url youâre trying or add the id
into the url.
Second note: Unless you have 100% control over the usernames, you will have problems doing this. If someone uses a url âunsafeâ character within their username, this will fail.
Hi i solved it by
filter(author__username=author)
Hi, how can i solve the unsafe characters with a username ?
If the purpose is to allow users to type in their username as part of the url, then youâll have to train the users to type in their âurl-encodedâ username. If youâre only using it as a link generated as part of a template, then you need to url_encode their username when generating that url, and url_decode the supplied parameter in your filter.
actually people can search for the profile page via the search bar and click on the results which they want and it will take them to the required user profile page with their respective posts
can you help me at the url_encode part. sorry a bit newbie here. dont want any compromises on my website by hackers or someone else ![:slight_smile: :slight_smile:](https://emoji.discourse-cdn.com/twitter/slight_smile.png?v=9)
See the Python urllib module, particularly the URL quoting functions. For the path component of the url, youâll want to use the quote and unquote functions.