Render template and I want to the url address change too - python

I from login page render to the admin index page, but the url address did not change, how to change it?
Code is below:
return render(request, 'app_admin/index.html')
As a common sense , all we know use render the url address do not change, but I want it change. how can I do that? I do not want to use the redirect, because I will pass data in render. how can I do that?

There is no way for the render to change the URL. Django uses the model-view-controller pattern, and the view has nothing to do with the URL. To achieve what you want you will have to redirect the user first, and then render on that route instead.
I see the URL on your page is /login so I assume this problem occur when you attempt to log the user in. If you reuse this approach (render upon receive submit form), you will run into problems sooner or later. For example, try to refresh the page you are currently displaying, I think the browser asks you to resubmit the form.
You say that you don't want to redirect because you pass data in the render. This is also an error prone approach. Store information consistently between pages with cookies and sessions. How would you otherwise store the login details when the user navigates the admin panel?
What you ask is not possible. The render function (with friends) has no option to change the URL.

You have to bind a separate url if you want to display in browser URL
For this
Add url for a index.hmtl and assign view to it
on action call just url what you specified

Related

Auto refresh HTML database data in Django - Ajax?

I have a Django server which is locally hosted and displays sensor data from a MySQL database. This data is displayed on the instruments.html page through variables such as {{qs.value}} which comes from the views.py, models.py and URLs.py. The views.py page is as follows:
from django.http import HttpResponse
from django.shortcuts import redirect, render
from .models import Sensorresult
def db(request):
qs = Sensorresult.objects.using('Vision').get(sensorresult='1')
return render(request, 'authenticate/instruments.html',{'qs':qs})
The problem I have is that I want the database information to update on the html every second, whereas currently it only updates when the page is refreshed. I know I could place a line of javascript at the top of the page and have the entire webpage constantly update, but I only want the database values to update. Does anyone have any suggestions as to how to do this?
From googling have come across Ajax, however this is something I have no experience in and I am unsure how my code would be edited to accommodate this?
Many thanks
You may want to consider using htmx.
Very simple library which takes away the JavaScript complexity to update parts of a web page rather than a full page reload and allows you to do most of the work in html.
Their docs specifically make reference to polling to update a certain part of the page which seems to be exactly what you’re trying to achieve.
Essentially all you’d need to do is create a view that returns a template partial, then use htmx polling to hit that url and replace a div with the partial the view returns.
Instead of directly using fetched data in HTML, you can try fetching it in javascript.
And if you have ever created the clock with javascript, it does not reload the entire page but still updates the time. You can use the same way to update that fetched data in HTML.

Django - redirecting to home page if no url matched

Basically, my goal is to redirect the user to the view that renders index.html if nothing from URLConf matched the requested url. Then, on the clientside, I would manage routing as appropriate or show 404 page.
So basically, the question is,
how to implement this redirecting in Django? I have tried this:
url(r'^(.*)$', views.index),
...some other urls
...and not surprisingly, (.*) means that ALL urls are redirected to index.html, no matter if there it matches any other URL or not. So should I just place this in the very end of the url list? I have a feeling that I should use some sort of middleware, or maybe Django has a native way to address this issue?
An umpleasant side-effect of this would be for users of my rest app. Each time they all an invalid URL, they would get HTML (that would in effect be the HTML for home.html) instead of json or 404 error. Well, nothing ugly about index.html code, and yet it sounds like a bad idea. So would I separate this logic so that the users of REST app get 404 error instead of being redirected to index.html? This is much about coding patterns, not Django itself. And yet I would really appreciate any comments on this as well :)
You can add the catch all at the end of your URL config, but it leads to some unexpected behaviour (e.g. redirects from /my-page to /my-page/ (appending slash) no longer work).
A better approach would be to write a custom middleware. You could look at the redirect middleware for inspiration since it handles redirects.
If you don't want to redirect API requests, you could add a check to your middleware. For example you could not redirect requests where the URL starts with /api/, or the request has an Accept: application/json header.
Another option would be to use a custom 404 handler that renders your index page. Again, in the handler you have the request object, so you can treat API requests differently.

How to keep request.referer value from redirect after a failed login

I'm adding authentication to an existing pyramid project. The simplest form that I'm currently trying (will be expending later) is for all pages to raise HTTPForbidden. The exception view is /login, which will ask for login details and, on success, return HTTPFound with request.referer as the location.
So far so good, this does what I want, which is bringing users back to the page they were trying to access when the login page interrupted them. Let's call this Page A.
The login page is a simple HTML form with a submit button.
However if the user mistypes username or password, I want to return to the login page with an error message saying "Wrong password" or similar. When that happens, request.referer is now the login page instead of Page A.
How do I 'store' Page A (or rather its URL) so that, when the user eventually succeeds in logging in, they find themselves back on Page A? Is the session used for things like this, and are there non-session ways of implementing it? I don't (yet) have a session for this simple page, and am trying to avoid adding different components in one pass.
I recommend you to pass a parameter like login/?next=pageA.html
If the login fails, you could then forward your parameter next to /login again, even if the referrer points now to /login.
Then when the user will successfully log in, you could redirect if to pageA.html that will be held in your next parameter.
You will indeed need to check if your parameter next is a valid one, as someone could copy-paste or try to tamper with this parameter.

Dynamic callback URL in Simpleauth

I cant figure out how to specify a dynamic callback url using Simpleauth (https://github.com/crhym3/simpleauth)
I would like to redirect user to whatever page that they were on prior to clicking on Login.
Can anyone help ?
There's two ways you can do this:
Store the previous page address in a cookie.
Pass in the previous page as a url parameter when going to the login page, and keep track of it through the login process. You might end up sticking it in a cookie.

Accessing Facebook Profile URLs

The goal here, given a user facebook profile url, access and open the profile page. Some simple python code:
from urllib2 import urlopen
url = "http://www.facebook.com/username"
page = urlopen(url)
The problem is that for some "username" this causes HTTP ERROR 404. I noticed this error only happening when the path includes a name rather than the "profile.php?id=XXX" format.
Notice that we only have the url here and not the user id.
UPDATE:
This turned out to happen also for some of the "profile.php?id=XXX" and other username formats.
This is a privacy feature of Facebook. Users have the ability to hide their profile page so that only logged in users can view their page. Accessing the page with /profile.php?id=XXX or with /username makes no difference. You must be logged-in in order to view the HTML page.
In your context, you'd have to first log in to a valid Facebook account before requesting the page and you should no longer receive the 404's.
One way to check this is on the graph API, graph.facebook.com/USERNAME will return a link property in the resulting JSON if they have a public page, and it will be omitted on private pages.
Not every Facebook account is accessible as FIRST.LAST, so you won't be able to reliably do this.
There is currently no guarantee that an account is accessible with a vanity name.
Works perfectly fine as long as the username exists.
Are you trying to open the page in a Web Browser or access the HTML source generated by the page?
If the latter, have you thought of using the Facebook Graph API to achieve whatever it is that you are doing? This will be much faster and the API is all documented. Plus the page's HTML source could change at any point in time, whereas the Graph API will not.
Edit
You could use the Graph API without having to even create an application to get the user ID, but going to http://graph.facebook.com/username and parsing the JSON response. You can then access the profile HTML using http://www.facebook.com/profile.php?id=userId

Categories