URL redirection if wrong url - django

I have this method in my book model:
def get_absolute_url(self):
return "/book/%s/%s/%i/%i/" % ( self.book_title, self.book_editor, self.book_pages, self.id)
So the urls of each book are like this:
example.com/book/the-bible/gesu-crist/938/12/
I want that if there is an error in the url, then I get redirected to the real url by using book.id in the end of the url.
For example if I go to:
example.com/book/A-bible/gesu-crist/938/12/
the I will get redirected to:
example.com/book/the-bible/gesu-crist/938/12/
How can I do that ?

You could create custom HTTP 404 handler.

Related

Permanent redirect absolute url

I am struggling with a basic redirect functionality.
I need to redirect all traffic not matching certain paths to another domain.
in my urls.py
re_path(r'^(?P<shortcode>[\w-]+)/$', core_views.myView)
and the corresponding function in views.py
def myView(request, shortcode=None):
url = 'www.newdomain.cz/' + str(shortcode)
return HttpResponsePermanentRedirect(url)
but what it does is - when called for example www.olddomain.com/sdfasd it redirects me to www.olddomain.com/sdfasd/www.newdomain.cz/sdfasd but I obviously need only www.newdomain.cz/sdfasd
what am I missing?
You need to use a fully qualified url.
def myView(request, shortcode=None):
url = 'http://www.newdomain.cz/' + str(shortcode)
See the doc here.

add postfix info to url django

I realize stackoverflow does a trick with the url for making it more human readable
It has the following pattern
stackoverflow.com/questions/id_question/title_question
for example we have
stackoverflow.com/questions/36676286/counter-with-lambda-over-map-java8
And if you delete part of the title question and you go to the url left
Example : stackoverflow.com/questions/36676286/counter-with-la
You are still redirect to the correct url.
Looks it only reads until the id, and then it adds the title info to the url , how I could add this information to the url?
Thanks in advance
You can achieve this by capturing both id and slug in the URL but only using the id to look up the post; then you can compare the post's slug with the one you got, and redirect if they are not equal. Something like (using Django 2.0 path syntax):
path('questions/<int:id>/<slug:slug>', views.question, 'question')
...
def question(request, id, slug=None):
post = Post.objects.get(id=id)
if slug != post.slug:
return redirect('question', id=id, slug=post.slug)
...

URL patterns for GET

I want to define url pattern for the below url and read these parameters in views
http://example.com/user/account?id=USR1045&status=1
I tried
url(r'^user/account/(?P<id>\w+)/(?P<status>\d+)/$', useraccount),
In Views
request.GET ['id']
request.GET ['status']
but it is not working, please correct me.
django url patterns do not capture the query string:
The URLconf searches against the requested URL, as a normal Python
string. This does not include GET or POST parameters, or the domain
name.
For example, in a request to http://www.example.com/myapp/, the
URLconf will look for myapp/.
In a request to http://www.example.com/myapp/?page=3, the URLconf will
look for myapp/.
The URLconf doesn’t look at the request method. In other words, all
request methods – POST, GET, HEAD, etc. – will be routed to the same
function for the same URL.
So, with that in mind, your url pattern should be:
url(r'^user/account/$', useraccount),
In your useraccount method:
def useraccount(request):
user_id = request.GET.get('id')
status = request.GET.get('status')
if user_id and status:
# do stuff
else:
# user id or status were not in the querystring
# do other stuff
Querystring params and django urls pattern are not the same thing.
so, using django urls pattern:
your url:
http://example.com/user/account/USR1045/1
urls.py
url(r'^user/account/(?P<id>\w+)/(?P<status>\d+)/$', views.useraccount)
views.py
def useraccount(request, id, status):

Redirect 404 error to other site

I want to redirect all pages with 404 error to another site example.com. I tried to write something like this:
handler404 = 'index.views.custom404'
def custom404(request):
return HttpResponseRedirect(reverse('index'))
But it doesn't work.
What should I write in urls.py and views.py for this operation?
Just write the fully qualified url:
handler404 = 'index.views.custom404'
def custom404(request):
return HttpResponseRedirect('http://othersite.com/custom404.html')

directing based on GET request parameters in django

I'm in c.html (http://localhost:8000/practice/c) and I click a link that changes my url to
http://localhost:8000/practice/c/?q=1
this is urls.py
url(r'^$', prac_c),
url(r'^q=\d',fetch_c),
and this is views.py
def prac_c(request):
return render_to_response('subject/c.html', {"problem_list":problem_list})
def fetch_c(request):
qno = request.GET.get('q')
if qno:
return render_to_response('subject/que.html', {'qno':qno}, context_instance=RequestContext(request))
but I'm not getting directed to que.html. Something wrong with the urls.py?
What the URLconf searches against
The URLconf searches against the requested URL, as a normal Python string. This does not include GET or POST parameters, or the domain name.
source