Below is the entry in urls.py
urlpatterns = patterns('',
# Examples:
url(r'^login/','juicy.views.login'),
url(r'^mylogin/',RedirectView.as_view(permanent=False,url="http:\\www.google.com")),
url(r'^admin/', include(admin.site.urls)),
)
And this is my template.
<html>
<head>
<title>Login Page</title>
</head>
<body>
<form action=\mylogin\ method = get>
<input type="submit" value="Go to Google">
</form>
</body>
</html>
Problem is whenever i Click the button "Go to Google" it tries to open the url
as
<code>
"http://localhost:8000/mylogin/%5Cwww.google.com".
<code>
Funny thing is it was working till yesterday evening. I was able to go to google.com. After clearing the cache/cookie and some other cosmetic modifications I re-run the server and wooah back to square one.
Any help is much appreciated.
"http:\\www.google.com" is not the URL you have in mind. Try it with forward slashes: "http://www.google.com".
More precisely, "\\" is being interpreted as a single escaped backslash, so your apparent URL is "http:\www.google.com". Without the // marker to separate the scheme from the hostname, that looks like a relative URL which should be resolved relative to the current page - which is exactly what you're seeing.
Fully escaping the "\\" sequence (or using a raw string, which don't interpret escape sequences) would not solve your problem - "http:\\www.google.com" is also a relative URL. I mention this mostly to explain why you're only seeing a single %5C sequence in your resolved URL. %5C is the hex for a backslash character.
You also seem to have backslashes in your form action - that should be <form action="/mylogin/" method="get">.
Related
I have run into a problem with static files after a redirect in Flask.
The web app I am writing will redirect users after logging in for the first time to a site where they need to change the default password they've been provided. On this site my custom stylesheets are entirely broken and images won't display either.
A quick inspection in the Chrome dev tools showed that after the redirect the content type for the all files from the static folder is set to text/html. I suspect that this is the issue.
In my template html the stylesheet is linked in the following way:
<link rel="stylesheet" href="{{ url_for('static', filename='css/colorVariables.css') }}">
The stylesheets and images work fine on every other page of the app and also if you go to the exact same page without the redirect.
The redirecting part looks like this:
if user.first_login:
return redirect(url_for("change_password"))
I have found the issue. The redirect was happening in a function run with app.before_request. Wrapping the routes with the function manually solved the problem.
I'm working on an html form page using a template which can be found here. The template works as expected, however when I include a URL parameter, the page loses its CSS.
Works:
#app.route('/add', methods=['GET', 'POST'])
def add_form():
return render_template('form.html')
Doesn't work:
#app.route('/add/<param>', methods=['GET', 'POST'])
def add_form(param):
return render_template('form.html')
I've worked with URL parameters in the past and never had this problem, what could be causing this behaviour?
Check if your CSS files are included with relative path f.e.
<link rel="stylesheet" type="text/css" href="css/main.css">
If there is no lead slash then files are relative to the given path.
In the first case, it will be relative to / (last part omitted)
in the second case, it will be relative to /add/.
Try change path from above to
<link rel="stylesheet" type="text/css" href="/css/main.css">
with a leading slash.
I have a question regarding why Django isn't matching my the url in an anchor tag in my template. I'm finding that in this one instance, if I put a button inside an anchor tag, Django won't match it because it looks for a trailing slash. If I put the button inside a form, it matches fine.
<button>Reset my account</button>
The above code returns a 404 error:
Using the URLconf defined in main.urls, Django tried these URL patterns, in this order:
^admin/
^ ^$
^ ^process$
^ ^checkout$
^ ^reset$
The current URL, reset/, didn't match any of these.
If I put the button inside form tags, it works fine:
<form action="/reset">
<input type="submit" value="Reset my account">
</form>
Alternately, if I add a trailing slash to the path in my_app/urls.py, the anchor tag will work:
url(r'^reset/$', views.reset)
Why is Django trying to match a trailing slash in this instance? I have used this approach before without issue.
Thanks!
We are making our school site on Django, we made a gallery app but i think the images are not getting uploaded to designated folder, rather they are not even getting uploaded i think so .
Code : -
# views.py
def upload(request, template_name="gallery/form.html"):
form = GalleryForm(request.POST or None, request.FILES or None)
if form.is_valid():
form.save()
messages.success(request, "image has been uploaded")
return redirect('gallery.views.index')
return render(request, template_name,{'form':form})
from django.db import models
# models.py
class GalleryPhoto(models.Model):
image = models.ImageField(upload_to='pic_folder/')
description = models.CharField(max_length=50)
def __unicode__(self):
return self.image.url
The rest of the code can be seen in this repo
Note : the the gallery template contains some conflicts but that is not the actual problem.
It took me a while to notice it, because it's kinda tricky. But in retrospect it's obvious.
So after configuring MEDIA_URL = "/MEDIA/" I noticed that http://127.0.0.1:8000/media/ was giving me a 404. If everything is configured correctly (and it is) then usually the problem is conflicting urls. See the url patterns used in django are always evaluated in order. That means if you have urls like so:
url(r'^mypage/', 'myapp.mypage'),
url(r'^mypage/another/', 'myapp.different')
Then the second url will never be available. Why? because there's nothing up there that tells the url dispatcher that the first url should end at mypage/. As far as it's concerned, everything starting with http://DOMAIN/mypage/ will be caught and passed to the first function.
To avoid that, we use the $ sign to tell the dispatcher at what point to stop. Like so:
url(r'^mypage/$', 'myapp.mypage'),
url(r'^mypage/another/$', 'myapp.different')
Now let's look at your last two urls:
url(r'^', include('pages.urls')),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
See that? url(r'^' swallows everything that comes after it, so no media is being served. It's true that pages.urls uses a dollar for its first url:
url(r'^$', views.index, name='index'),
but that doesn't matter for the main url dispatcher, which evaluates r'^' first. What's the solution? the simplest would be to move the pages urls to the end:
url(r'^ckeditor/', include('ckeditor.urls')),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += patterns('', url(r'^', include('pages.urls')))
You can also isolate that one problematic url from the others using a direct call, like so:
url(r'^/$', 'pages.views.index'),
url(r'^pages/$', include('pages.urls')),
After that, this will work:
<img src="{{ MEDIA_URL }}{{ image.image.url }}" alt="{{ image.description }}">
p.s.
Eventually you're going to pass the media and static files through your server, when you move to deployment level, so maybe the whole thing wouldn't matter then. If you do decide to keep the r'^' in use, just keep in mind that it might cause conflicting problems with other urls, so be sure to always put it last.
When I try to use a link in my Django template from /appname/index/ to get to /appname/detail/### I am instead getting to /appname/index/detail/### which is not what I'm trying to get so my app can't find it in the urlconf of course.
First the urls.py line for the detail page
url(r'detail/(?P<jobID>\d+)/$', 'appname.views.detail')
Additionally, the root urlconf
urlpatterns = patterns('',
url(r'^appname/', include('appname.urls')),
url(r'^admin/', include(admin.site.urls)),
)
Next the template code trying to get there
{% for job in jobList %}
{{ job.name }}
I'm not sure what else might be applicable information, just ask if you would like to see something else. I also tried :
{{ job.name }}
But that didn't work either. Thank you in advance for any help.
Add / at start in href:
{{ job.name }}
And for the url tag to work you need to do it like this:
{{ job.name }}
From my experience, as long as you have defined the url of the page the href tag should lead to in urls.py, include the absolute path in the format below.
Site name: yyyy.com
Url of page to redirect to in urls.py: yyyy.com/signup/
Sample link: Signup
You will notice that what goes inside the href tag is sort of appended to the current url. For more dynamic links, you can use some python as of DTL guidelines.
Suppose u are at
myapp/detail
and u want to go at page
myapp/anything
U shoud do this
Page
For django templates
Url.py File
app_name = 'app_name'
path('url/', TestView.as_view(), name='the_url'),
path('url//<int:id>', TestView.as_view(), name='the_url'),
templates(html.py)
Test
Test
I got the same problem, new address is being added after the "/" URL which is not recognised.
Can be solved by adding "/" at the address you are giving at the href attribute.
ex: href:services is wrong
href:/services is to be used to go to services page.