I am facing a problem: I want to give a link in my change form that will redirect to a page which may be simple php page also or any page, in that page I want to perform some db queries and display them. I also wan to pass id on click. Is it possible?
In my view.py I wrote:
from django.shortcuts import render_to_response
from django.template import RequestContext
def MyClass(self,id,request):
return render_to_response('admin/custom_change_form.html')#my template location
My model and admin files are simple.
To send to a file directly, use direct_to_template(). You can pass anything in the url that you like - just give the information to your template, and write it in the url. After all, Django doesn't require url helpers.
I sense that whatever you're trying to do is some god-awful hackish thing that would be much better served by doing it all in Django.
You will need to override your change form for that model and display whatever you would like. But that is making the assumption you are talking about the contrib admin within Django.
Realistically you have not provided sufficient information for anyone to accurately answer your question.
Related
I'd like to know why is happening this.
I open my Django project (at 127.0.0.1:8000) and it appears my index
page as I've set it in URLs and views.
At the top I have a piece of code that if I'm authenticated it
appears some text, and if not, another one.
It appears when I open the index page that I'm not authenticated, but
when I click a link in my menu to other page in my project, it
appears that I'm actually authenticated.
I'm looking in the web for possible explanations but can't find out an answer to why I'm authenticated or logged in in a page and then in another not.
How could this be possible, and how could I manage it?
Check for #login_required decorator over your view functions. The documentation is at:
https://docs.djangoproject.com/en/1.9/topics/auth/default/#the-login-required-decorator
It is difficult to answer your question without seeing your code, but it sounds like you are not passing the request context to your index view. Make sure you are using the render shortcut in your views:
from django.shortcuts import render
def index(request):
return render(request, 'index.html')
I'm currently using out-of-the-box django.contrib.auth to handle authentication in my Django app. This means that the user starts at a log in page and is redirected to the app on successful login. I would like to make my app single-page, including this login process, where a redirect doesn't happen, but maybe a "hot" template switch-out or some fancy client-side div magic (that still remains secure). My Google searching turned up pretty short, the closest solution dealing with putting a log in form on every page.
Any direction or ideas here would be much appreciated. I would obviously prefer to work within the existing confines of django.contrib.auth if possible, but I'm open to all solutions.
I'm not sure I understand your question completely. I think you want to have a single page. If so, put logic in your template that checks to see if the user is authenticated. If not, display a login form that POSTS to the appropriate django.contrib.auth view. You can supply an argument to this view to have it redirect back to your page. When you come back, the user will be authenticated, so you won't display the login form.
Have a look at Django-Easy-Pjax https://pypi.python.org/pypi/django-easy-pjax - it works like a charm and is well documented. Everything you like is being made with AJAX requests: links, forms using GET and forms using POST.
Essentially you only need to add a data-pjax="#id_of_the_container_where_the_result_goes" attribute in your a and form tags.
And the great thing about it: It updates the title and location bar of your browser.
One caveat: If you want to upload files in some form, this is not supported by Easy-Pjax, so you might want to use some workaround jQuery library for that.
I'm trying to implement a generic view that performs some pre-processing and redirects to an external website. Essentially I want to query and update the database before performing the redirect each time the view is accessed.
I spotted Python + Django page redirect but this only deals with simple redirects and not with generic views.
Can anyone provide an example on how this should be implemented?
Any help you can give would be greatly appreciated,
Niall
If you just want to do some processing before performing a redirect, I'm not seeing how generic views come into play for you. You can customize generic views by wrapping them in another function, or in the case of class-based generic views, subclassing them. In your situation, I fail to see how they'd benefit you much, just write a normal view, and use redirect:
from django.shortcuts import redirect
def my_view(request):
...do some processing
return redirect('/some/url/')
Not going to get easier than that.
I have been doing CRUD operation in Django but I am having problem with what to do after successful operation.
Is there any way that i can redirect the user to Home page with small message on top of page like
"Record Successfully added or something else" like Google does with Dismiss hyperlink :)
EDIT:
Actually that a is full documentation and not a simple example.
I could not understand in which template I have to include {{messages}} in. In update form, edit form or home page form?
I don't understand where I need to define messages. I use only generic views so I am confused.
Use the django.contrib.messages framework to get the same messaging style as used in Django admin.
Enable the middleware module by adding 'django.contrib.sessions.middleware.SessionMiddleware' to MIDDLEWARE_CLASSES in settings.py.
In your view, use something like messages.add_message(request, messages.INFO, 'Stuff happened') to record information where it makes sense.
In your template, you'll have a list messages which you can iterate over where it makes sense in your design - the example in the documentation shows a full example which exposes the tags as CSS classes for ease of styling.
I have imported an HTML file which displays fine --
def index(request):
html = open('index.html')
return HttpResponse(html)
However, the CSS import and image references are not working (even though the files are there and it works when I test the file outside of Django). What do I need to do so that the imports/references within the HTML work? Thank you.
Not sure if you've read through Django's docs, but I think what you probably want is this:
from django.shortcuts import render_to_response
def index(request):
return render_to_response('index.html')
EDIT: Take a read through this: http://docs.djangoproject.com/en/dev/intro/tutorial03/
I have a feeling that your problem may be related to media and/or static file hosting. If you view the page source and click on your links, do they go to Django's 404? How are you hosting your files?
If you are just working on development on your local machine take a look at the docs for static file hosting.
Also- if there isn't any logic in your view, other than html rendering, check out direct_to_teplate generic view which allows you to go straight from your urls.py to a template without writing a view.
Good luck!