relative URL seems to be interpreted incorrectly by the browser - href

I have a relative URL on a page, which is being interpreted relative to the root (instead of relative to the current doc). Can't figure out why.
On page:
http://vm:8080/docs
There is a tag:
<a rel="nofollow" href="_sources/index.txt">Show Source</a>
The browser resolves this to:
http://vm:8080/_sources/index.txt
Instead of:
http://vm:8080/docs/_sources/index.txt
Here is some information for the anchor from the DOM.
origin http://vm:8080
baseURI http://vm:8080/docs
pathname /_sources/index.txt
href http://sanjay-vm:8080/_sources/index.txt
Why is the browser adding a leading slash to the pathname?

Related

django url concatenation. I don't want to concatenate

when I do something in 'profile' page,
the url is concatenated to the next of 'profile'.
but I want to link to just 'signout'. not 'profile/signout'
this is my urls.py.
when ever I do something in 'profile'page,
the href link is concatenated to 'profile'url.
this is href source.
since this href source is header.html,
this page is included another pages.
and in the other pages, it works well.
only in profile page, the href url is concatenated to 'profile/1' url.
how can I fix it?
Yes, a URL not starting with a slash or a scheme is a relative URL. href="foo" is equivalent to href="./foo", i.e. it refers to the path foo relative to the current path. If you want the top-level path, you want href="/foo".
In Django you're supposed to use the {% url %} template tag to generate URLs, you don't hardcode them. Django will take care to generate the correct URL; especially if you move the app around to other environments, the URL may require a prefix or such, so you should never hardcode the URL.

Using an anchor tag to link to another part of my webpage

Here is my code:
Click to go to Cars
For some reason (I'm working in sublime) the first % and the u in the URL are purple (instead of the yellow it should be because it is in a string) and when I run my webapp and click on the button the error is:
Page not found (404)
Request URL: http://127.0.0.1:8000/webapp/%7B%25%20url%20'cars'%20%25%7D
And it is looking at all my mysite.urls URL patterns and sees my URL pattern
For webapp/ which is working because my index page shows up.
it says the current URL is: webapp/{% url 'cars' %} which is not what it should be and I believe has something to do with the % and you in URL being purple instead of yellow in my text editor.
I have also used regular anchor tags, but they don't update the URL they just add onto it so when I updated it to add the cars/ at the end, my other page did show up so it seems to be simply a syntax problem. My idea is maybe it has something to do with %u being a special character or something.
here is my url pattern in webapp:
urlpatterns = [
url(r'^$', views.index, name ='index'),
url(r'^cars', views.cars,name='cars'),]
here is my url pattern in mysite:
urlpatterns = [
url(r'^admin/',admin.site.urls),
url(r'^webapp/',include('webapp.urls')),]
I added the quotes and got the same error as before.
The text's color in sublime depends on the selected language you're editing, for Django Template Language, you need a plug-in, it does not matters.
I think you're missing the last '"', it should be:
Click to go to Cars

URL and regex with django

I have a problem with the urls.py in my django project
I have an url like this:
http://127.0.0.1:8000/cars/?page=2
then when i clik on next it will be :
http://127.0.0.1:8000/cars/cars?page=3
and I have en error :(
the url is :
url(r'^cars/$page=n', TaskViewSet.as_view()),
This is happening because you are linking next/previous pages like <a href="cars?page={{ page_obj.next_page_number }}"> in your template.
You should make following changes to your project:
Rename url conf, like #jonatron suggested:
url(r'^cars/$', TaskViewSet.as_view()),
Then in your templates, make sure you link next/previous pages with their absolute positions, like this:
next
Also, you can name your url conf and generate the full url without having to rewrite everytime:
url(r'^cars/$', TaskViewSet.as_view(), name='cars'),
next
You need:
url(r'^cars/$', TaskViewSet.as_view()),
Anything after the ? is a query string, which does not get matched in the URL patterns. You can access the ?page value in the view, eg:
page = request.GET['page']

Mask forwarded blogger urls to own domain urls

Following Rounin's answer carefully written (thanks a lot) on how to redirect any blogspot urls with any extension to the mydomain.com corresponding URL, now the question is how can I mask the URL?
I mean, once the blogspot URL redirects to the mydomain.com, I want to continue to display the original blogspot URL instead of the mydomain.com.
You can use the following JavaScript snippet for that -
<script>
site = "http://example.com"; // The site which you want to mask, don't add ending slash
iFrame = document.createElement("iframe"); // Creates a iframe via JavaScript
iFrame.setAttribute("src", site + location.pathname); // Set the source of iFrame
iFrame.setAttribute("class", "maskingFrame"); // Add class to iFrame
document.body.appendChild(iFrame); // Append iframe to body of page
</script>
And the bare minimal CSS would be -
body {
overflow:hidden;
}
.maskingFrame, body {
width:100%;
height:100%;
border: none;
}
You can check a demo here (This is the homepage) and here (This is an internal URL from other site which doesn't exist on the original blogspot URL)
In privous answer you redirected page from blogspot to your domain. This causes the url to be changed. But if you want to show contents from another url without changing url it could be done through using .htaccess file.
the code in htaccess file should be like this:
RewriteCond %{HTTP_HOST} ^DomainA.com
RewriteRule ^(.*) http://DomainB.com/$1 [P]
Here you could find more details and info about .htaccess file.
I don't know if it is possible for you to place that file in your blog or not. If you have not access to place this file into your blog you can place it on your domain host, and redirect from your domain to your blogspot page but if ask me I recommend you redirect and encourage people to your own website rather than keeping them using weblog address. You'll not need weblog if you have your own website.

Django url pattern doesn't match

I am using new Django 1.8 app to learn Django.
I am stumped as to how to get this my simple url to be resolved by urls.py
I create the url in another view as:
<a href="/photoview/{{photo.id}}/"}>
I can successfully pass this url to the browser as:
http://localhost:8000/photoview/300/
I am expecting that this url can be matched by the urls.py expression:
url('r^photoview/(?P<id>\d+)/$', views.photoview),
But this is not working. I have tried variations of this but none have worked so far, such as:
url('r^photoview/(?P<id>[0-9]+)/$', views.photoview),
I get this message in browser when it fails to match
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/photoview/300/
Using the URLconf defined in asset.urls, Django tried these URL patterns, in this order:
^admin/
^$ [name='index']
^time/$
^about/$
^accounts/$
^photos/$
^tags/$
^users/$
r^photoview/(?P<id>\d+)/$
^static\/photos\/(?P<path>.*)$
The current URL, photoview/300/, didn't match any of these.
Appreciate any help getting this to work.
you have url('r^photoview/(?P<id>\d+)/$', views.photoview),
you want url(r'^photoview/(?P<id>\d+)/$', views.photoview),
(Note the r is in front of the string, not the first character)
As noted in docs.djangoproject.com/en/1.8/topics/http/urls,
The 'r' in front of each regular expression string is optional but
recommended. It tells Python that a string is “raw” – that nothing in
the string should be escaped
Also note that you should use a friendly name in your url definition (e.g. photoview) and then use {% url 'photoview' photo.id %} in your template instead of hardcoding the URL pattern.