Django reload current page without using path - django

this is probably a noobie question.
I'm working on a Django project, after a request, I reload the page using:
return redirect('home')
Is there anyway to reload the current page without writing the url path? I don't need to specify it, since the page I need to reload is the one that is actually open.
Hope someone can help and sorry for easy english

You can use the following code :
from django.http import HttpResponseRedirect
return HttpResponseRedirect(request.path_info)
Your request contains the actual path so you can redirect to the same page without specifying explicitly
Path Info Django Doc

Related

Serve static HTML in Django

I’m pretty new to Django so forgive me if this is something I shouldn’t even consider, but I’d like my app to be able to link to a large number of static HTML pages (enough that creating URL paths to each one would be unreasonable) and there would constantly be more being uploaded (by me via FTP).
I’ve got this working on the the development server by adding the path to those HTML files to my STATICFILES_DIRS [] but that doesn’t seem to work when I push my code to production.
I tried setting up a STATIC_ROOT and running collectstatic but that didn’t help, and I’m also worried that even if I got that working, I would have to run collectstatic each time I uploaded new files.
So my question is, is this even reasonable? Should I just avoid hosting these static HTML files alongside my project and leave them where they are now, on a separate server under a separate domain?
The only reason I wanted to host them together initially is because along with the static HTML files, there is an SQL LITE database that my Django app displays data from (this is the whole purpose of my app). However, I could just implement another method of getting that SQL LITE file like using ftlib. Note that I don’t need that database to connect to Django at all - I just needs to read data from it.
You don't need to write urls for every page. You can "capture" the requested page name from the url and render the page according to its value.
# urls.py
url(r'^page/(?P<page_name>\w+)/$', my_view)
# views.py
import os
from django.http import HttpResponse, Http404
FTP_UPLOAD_DIR = '/path/to/directory/where/you/upload/files/'
def my_view(request, page_name):
# check if requested page exists
if os.path.exists(FTP_UPLOAD_DIR + page_name):
# if yes, then serve the page
with open(FTP_UPLOAD_DIR + page_name) as f:
response = HttpResponse(f.read())
return response
else:
raise Http404
Above, we are reading the file directly from the upload folder, so there's no need for you to run collectstatic.

I get authenticated in some Django pages and in some not

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')

How are Django page templates assigned to each page?

I couldn't find this info in the Django docs, but I'm sure it is there, I'm just very new and don't know what terms/etc to search on.
How are Django page templates assigned to each page?
I have a login to a Django site, and also SFTP access to the site. I don't think my Django login is a superuser/full-admin though because the interface seems pretty limited compared to other CMS systems. I can edit pages, posts and the media library, but I don't see anything that says how each page is assigned a template.
For example, I have this file /mysite/templates/pages/index.html
I know that template is being used for the home page because it has all of the content that is specific to the home page on it, and changes I make show up on the home page.
I tried copying that file to test.html, but when I browse to test.html in my browser, I get a 404 error (I also get that error if I go to index.html). So there must be something else that maps a template to a page, but I'll be dambed if I can find it. Will I need more access to the admin area, or can I do something with SFTP? I also have SSH access but wasn't able to follow any of the steps online to create a new superuser account for me, for Django.
Edit: Thanks for both answers, after I work through this I'll accept whichever helped the most. I do not have a views.py file, but I think it might be using an extra module for this routing, I have this in my urls.py file:
urlpatterns = patterns("",
("^admin/", include(admin.site.urls)),
url("^$", "mezzanine.pages.views.page", {"slug": "/"}, name="home"),
("^", include("mezzanine.urls")),
)
Is this "mezzanine" something different which changes the answer (location of views.py or list of views)?
url.py is the file that maps the urls to methods that return rendered templates. In essence you define the url and a method and when someone goes to that url, that method gets called which returns a HTTP response with the rendered template. This map is called urlpatterns. In the following example when someone goes to yourwebsite/blog then in the blog apps, view.py, page method is called, which will use a template and render that with specific information.
urlpatterns = patterns('',
url(r'^blog/$', 'blog.views.page'),
url(r'^blog/page(?P<num>\d+)/$', 'blog.views.page'),
)
Have a look at this link.
https://docs.djangoproject.com/en/dev/topics/http/urls/
Django uses urls.py files to map paths to views. This match is resolved using regular expressions. When a match is found, Django executes the associated view (usually inside views.py). The view is in charge to render the template required for the path (by finding it on the server's hard disk and loading it).
All aforementioned means that there's no direct association between a url path (i.e www.example.com/path/to/page) and a file on the server's hard disk (i.e /server/path/to/page). It's all performed dynamically by Django's engine when a request comes in.
If you want to know which view is gonna be generated for a specific path, follow the regexs at urls.py until you find the path you're looking for. Then open the view for that url and see inside which template it is rendering.
Reading doc's URL Dispatcher is a good point to start learning about this.
Hope this helps!

Importing HTML in Django

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!

Redirecting to a page

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.