Django - trailing slash resets page title - python

I apologise for the blatant ignorance of this question but I've been charged with fixing something in Django that I have NO experience with!
We're getting an issue with URLs and duplicated content.
If we visit "www.hello.com/services/" then we get our full page rendered, absolutely fine.
If we visit "www.hello.com/services" then we get the same content but with a default that seems to be set in a line:
class PageTitleNode(template.Node):?
?
def render(self, context):?
try:?
meta_info = MetaInfo.objects.get(url=context['request'].path)?
except ObjectDoesNotExist:?
return u'This is our default page title'?
return u"%s - hello.com" % meta_info.title
The main problem with this is that Google is indexing two almost identical pages and it's bad SEO according to our client's overpaid online strategy partner.
I know it's vague but if anyone can help then much rejoicing will be had.
Thanks for reading!

I think your consultant is correct. One URL = one resource. Having two urls on one resource is quite dirty anyway. This is why Django features automatic redirect from non trailing slash to urls with trailing slashes. Under certain conditions.
I'm pretty sure your url definition regexp for /services/ lacks the trailing slash. Anyway, you should use trailing slashes only:
Ensure APPEND_SLASH is set to True: from django.conf import settings; print settings.APPEND_SLASH
Ensure that all your url regexps have the trailing slash, e.g. url(r'foo' ...) is bad, and url(r'foo/' ...) passes barely because of possible conflicts and url(r'foo/$' ...) is better
Ensure all MetaInfo objects have url with trailing slash, e.g. MetaInfo.objects.exclude(url__endswith='/') should return MetaInfo without trailing slash in url

Related

Regular expressions and russian symbols in Django

I have the one url like this
url(ur'^gradebook/(?P<group>[\w\-А-Яа-я])$', some_view, name='some_view')
and I expect it to process a request like
../gradebook/group='Ф-12б'
but I get an error and the server crashes.
Please help me figure out the Russian symbols
The group='…' part is more a problem, since the equation sign = is not part of the character group.
Furthermore you should match multiple characters:
# quantifier &downarrow;
url(ur'^gradebook/(?P[\w\-А-Яа-я]+)$', some_view, name='some_view')
then this can match a URL:
/gradebook/Ф-12б
but if you want to match the group='…' as well, you should include the = and the ' character:
# extra characters &downarrow;&downarrow;
url(ur"^gradebook/(?P[\w\-А-Яа-я'=]+)$", some_view, name='some_view')
Then you can match with:
/gradebook/group='Ф-12б'
although that might accept too much, since it can also accept f'q'a=gr=f for example.

How to replace %20 with - in django urls

I have a url which contains %20. I want to replace it with - with the help of regex
I already tried replace method
url(r'^timeanalysis/(?P<name>[\w|\W]+)'.replace('%20','-'), timeseries.timeanalysis, name='timeanalysis')
I don't want to change my database.
This is something you need to fix at the point you generate the link, not in the urlconf.
Usually you would use a SlugField that is automatically pre-populated from the title/name, replacing spaces with dashes as necessary.

variables with space in url (django)

I am having the same issue as How to pass variables with spaces through URL in :Django. I have tried the solutions mentioned but everything is returning as "The resource you are looking for has been removed, had its name changed, or is temporarily unavailable."
I am trying to pass a file name example : new 3
in urls.py:
url(r'^file_up/delete_file/(?P<oname>[0-9A-Za-z\ ]+)/$', 'app.views.delete_file' , name='delete_file'),
in views.py:
def delete_file(request,fname):
return render_to_response(
'app/submission_error.html',
{'fname':fname,
},
context_instance=RequestContext(request)
)
url : demo.net/file_up/delete_file/new%25203/
Thanks for the help
Thinking this over; are you stuck with having to use spaces? If not, I think you may find your patterns (and variables) easier to work with. A dash or underscore, or even a forward slash will look cleaner, and more predictable.
I also found this: https://stackoverflow.com/a/497972/352452 which cites:
The space character is unsafe because significant spaces may disappear and insignificant spaces may be introduced when URLs are transcribed or typeset or subjected to the treatment of word-processing programs.
You may also be able to capture your space with a literal %20. Not sure. Just leaving some thoughts here that come to mind.
demo.net/file_up/delete_file/new%25203/
This URL is double-encoded. The space is first encoded to %20, then the % character is encoded to %25. Django only decodes the URL once, so the decoded url is /file_up/delete_file/new%203/. Your pattern does not match the literal %20.
If you want to stick to spaces instead of a different delimiter, you should find the source of that URL and make sure it is only encoded once: demo.net/file_up/delete_file/new%203/.

Recieving string at Views from Urls

All I want to do is pass the 'koala 2-2' string from the url to the views. currently it returns a blank string in sitename.
The URL:
http://127.0.0.1:8000/site/koala 2-2
urls.py:
url(r'^site/(?P<sitename>)', site),
views.py
def site(request, sitename'):
return HttpResponse('sitename: {}'.format(request))
You have to use (...) to capture what you want. In your case, use (.+) to capture everything.
There is also an error in your url : It can't have space
Then use :
http://127.0.0.1:8000/site/koala-2-2
and capture koala-2-2 with :
url(r'^site/(?P<sitename>(.+)', site),
If you want something more specific, (E.g: allow only word character and -, then use this one :
url(r'^site/(?P<sitename>([\w-]+)', site),
Thank you for the help guys, below is the final working solution:
urls.py:
url(r'^site/(?P<sitename>(.+))', site),
views.py:
def site(request, sitename):
return HttpResponse('sitename: {}'.format(sitename))
It doesn't seem to care at all about a space being there, it returns the string properly either way.
I searched for an answer to this for about 3 hours before I posted, https://docs.djangoproject.com/en/1.8/topics/http/urls/ has the answer there, but I missed it several times. I sure wish they would use less complex examples.

Tuning of a Web handler regex routes configuration

In a web handler routes configuration I have the following Regex:
('/post/(\w+)/.*', foo.app.WebHandlerFooClass)
this regex matches these kind of urls:
/post/HUIHUIGgS823SHUIH/this-is-the-slug
/post/HUIHUIGgS823SHUIH/
passing the correct HUIHUIGgS823SHUIH Id parameter to the web handler matched by the (\w+) group.
How could I modify the above Regex to match also this url?
/post/HUIHUIGgS823SHUIH
The handler is coded to accept just one parameter, the base64 Id, so there should be just one group in the Regex that matches the Id.
So, these are the urls that should be matched:
/post/HUIHUIGgS823SHUIH/this-is-the-slug
/post/HUIHUIGgS823SHUIH/
/post/HUIHUIGgS823SHUIH <-- Hey, I wanna this too
'/post/(\w+_-)(?:/([\w-]+))?/?'
This matches the following.
/post/HUIHUIGgS823SHUIH/this-is-the-slug
/post/HUIHUIGgS823SHUIH/this-is-the-slug/
/post/HUIHUIGgS823SHUIH/
/post/HUIHUIGgS823SHUIH
I think this is a better implementation because it captures only the pieces you want, e.g. the slug doesn't capture a trailing /. However, your spec is still slightly unclear to me, so this may not be your intention.
If you don't care about the data at the end, then why not just use this?
'/post/(\w+).*'
Otherwise you'll have to provide more info.
I think you just want:
'/post/([^/]+).*'
But that seems too simple an answer :)
If I guessed right your real intention, then you are fine with this one:
'/post/(\w+)'

Categories

Resources