I am having trouble to open a web page at the specific position from django view. After submitting a form, I wish it display where it was, but I cannot know where it has to be on the template itself, since that information is in form values.
Sounds like you need to define your success url as the same page from where the form was submitted, plus an anchor.
For example:
from os.path import join
class MyFormView(FormView):
template_name='some_form_template.html'
def get_success_url(self):
"""
Returns the supplied success URL.
"""
return join(reverse('viewname'), '#anchor')
In your some_form_template.html template:
<a id="anchor">Where you'd like the user to be dropped on the page.</a>
If your form validation was successful, the page will refresh and the user will be dropped to the anchor specified on that template.
Related
I am creating a simple eBay like e-commerce website to get introduced with django. For removing an item from the watchlist, I placed two same links in two different HTML files, that is, I can either remove the item from the watchlist.html page or either from the item's page which was saved as listing.html. The url for both the pages look like this:
Remove from watchlist
Now, in my views.py, I want to render different pages on the basis of the request. For example, if someone clicked Remove from watchlist from listing.html then the link should redirect again to listing.html and same goes for the watchlist.html.
I tried using request.resolver_match.view_name but this gave me 'removeFromWatchlist' as the url namespace for both of these request is same.
Is there any way I can render two different HTML pages based on the origin of the url request?
Also, this is my second question here so apologies for incorrect or bad formatting.
You could check the HTTP_REFERER in the request.META attribute of the view to get the url that referred the request as so:
from django.shortcuts import redirect
def myview(request):
...
return redirect(request.META.get("HTTP_REFERER"))#Or however you prefer redirecting
https://docs.djangoproject.com/en/3.1/ref/request-response/#django.http.HttpRequest.META
I'm learning Django now and everytime I use an UpdateView or anything that changes the content of a form I always have to use other html files that have the form like author_update_form, is there a way to edit directly on the same page like how on youtube comments I can press on edit and I dont get directed to a different page?
A very simple way is to display the information as a form, prefilled with the object attributes and then save the instance when you receive post request.
form = ObjectForm(instance=object)
if(request.method == "POST"):
form = ObjectForm(request.POST, instance=object)
form.save()
I'd like to create an app made of completely dynamic pages configured by JSON.
I chose ngx-formly for creating dynamic forms, it works nicely, but I could not figure out how to make a step forward and create the screens dynamically, while being able to add dynamic templates and pass form configuration to it. Is it possible? I appreciate every help, advice or code samples.
Goals:
Build an app completely from JSON configuration. AppComponent gets the JSON config on init, builds screen components with given component type, template and data, adds them to router config, and displays Home page.
NOT losing AOT
Simplified config:
import BaseFormScreen, FormScreenLayoutComponent, BaseScreen, HomeScreenLayoutComponent from '';
export const screens: [
{'title','Form1', 'path':'/form1', 'type':BaseFormScreen, 'template':FormScreenLayoutComponent, 'formdata':{...}},
{'title','Form2', 'path':'/form2', 'type':BaseFormScreen, 'template':FormScreenLayoutComponent, 'formdata':{...}},
{'title','Form3', 'path':'/form3', 'type':BaseFormScreen, 'template':FormScreenLayoutComponent2, 'formdata':{...}},
{'title','Home', 'path':'', 'type':BaseScreen, 'template':HomeScreenLayoutComponent},
]
[BaseScreen] provides base logic and data to its template, i.e. title, logging, services (smart comp, no template)
[BaseFormScreen] extends BaseScreen, builds a dynamic form from input json, and adds submit functionality
[HomeScreenLayoutComponent] template-only component would render navigation buttons on the page, one for each screen,
[FormScreenLayoutComponent] template-only component renders a title, a dynamic form, a submit button, and adds a 'home' button
Currently, I have routing set up in my project. Every new user is redirected to:
localhost:4200/default-path/login
Once the user logs in, they're redirected to the home module, which has some child routes the user can navigate to.
localhost:4200/default-path/home
localhost:4200/default-path/home/account
localhost:4200/default-path/home/account/profile
localhost:4200/default-path/home/dashboard
... and so on.
Now, the part in bold is already set up in the home module, and doesn't need further editing. However, I'd like to change default-path to something else.
Say, each user can belong to one of three projects: Project blue, red or green. This information is available only after the user submits their credentials and hits the login button.
What I'd like for the routes to say after login is:
localhost:4200/project-blue/home
localhost:4200/project-red/home
localhost:4200/project-green/home
Bear in mind, the parts in bold are NOT different modules with different associated components, they're just constants I'd like to see in the URL.
Currently, my <base> tag in the index is like <base href='/'> I'm trying to edit this attribute. What I've got is something like:
processLoginSuccess(loginTicket): void {
const routeName = loginTicket.baseUrl // returns 'project-red'
let b = document.getElementsByTagName('base')[0]
b.setAttribute('href', routeName )
this._router.navigate(['/home']);
}
But this doesn't seem to work.
Any ideas?
Instead of setting the base element's href value, you can set the base URL programmatically, by providing for APP_BASE_HREF with your custom operation.
This has been already discussed here: https://stackoverflow.com/a/41674411/415790
I have used template/login.html for logging in, after successful logged in how to redirect the login.html page to chatwindow.html page (some other html) in Django framework.
So, do I need to redirect it from views.py file or from login.html?
Any help highly appreciated.
Django implements multiple ways to do this:
1. LOGIN_REDIRECT_URL
This is a global setting in your settings.py. It works for all login pages when next= parameter is not specified in login url (e.g. example.com/login?next=/foo).
2. Use next parameter in the login URL
This is usually used to customize the login redirect for individual cases.
The common use-case is when you use #login_required decorator. When a user tries to access a page which requires authentication, a user is then redirected to a login page with a next= parameter pointing to the current page. For example if the user went to /secure/page, then the login page will be something like /login?next=/secure/page. After the user will successfully authenticate, Django will redirect them back to the protected page.
3. Use the hidden input next on the login page
Finally you can set the redirect path in the login form itself:
<form method="POST" ...>
<input typy="hidden" name="next" value="/secure/page">
...
</form>
I would guess that first method might be the most appropriate in your case however keep in mind the other options if you will need them.