Django static page? - django

I want to make a static page which will be shown to the user only if he/she clicks on a link provided in one of my models. I can do this by making a Python page alone and calling it, but I want it be called from Django. The user interface should be constructed using the Django API only.
Any suggestions?

With the class-based views in newer Django versions, one can use this in urls.py:
from django.views.generic import TemplateView
url(r'^about',
TemplateView.as_view(template_name='path/to/about_us.html'),
name='about'),

Bypassing views to render a static template, add this line in "urls.py". For example "About Us" page could be
(r'^about', 'django.views.generic.simple.direct_to_template', {'template': 'path/to/about_us.html'}),

Do you mean something like Django's flatpages app? It does exactly what you describe.

If you want to make a static page the flatpages is a good choice. It allows you to easily create static content. Creating static content is not harder than creating a view really.

On Django 2.2.6, loosely following David's answer, I added the path in urls.py:
from django.views.generic import TemplateView
urlpatterns = [
.... .... ....
path('about',
TemplateView.as_view(template_name='path/to/about_us.html'),
name='about'),
And I needed to adjust settings.py to specify the template directory:
TEMPLATES = [{
... ... ...
'DIRS': [os.path.join(BASE_DIR, 'template')],
Then I saved the actual content in template/path/to/about_us.html

Related

Add extra context to a Django Flatpages template

The flatpage() view in Django's Flatpages app passes a single context item to templates, flatpage. I'd like to add more data to the context and the only way I can think of is to copy both the flatpage() and render_flatpage() functions from the original views.py into a new app. And then in my urls.py where I have this:
from django.contrib.flatpages import views
from django.urls import path
urlpatterns = [
path("about/", views.flatpage, {"url": "/about/"}, name="about"),
]
instead import from myapp import views to use my new custom view.
Both of my copies of the functions would be exactly the same as originals, except render_flatpage() would add more context data.
This seems over the top, copying so much code unchanged. But I don't have a better idea.
I don't want to create a custom context_processor for this, because this context is specific to each Flatpage, and should not be used on on other pages.

Simplest way to add a view to my Django admin UI?

What is the simplest way to add a view to my Django (3.2) admin UI? That is, add a URL mysite.com/admin/my-view so that visiting that URL acts like the rest of admin (in particular, requires similar permissions).
There is a whole page on this, but it's not obvious to me how to piece it together.
https://docs.djangoproject.com/en/3.2/ref/contrib/admin/#adding-views-to-admin-sites says you can add a get_urls to your AdminSite class. Okay, so I need my own AdminSite class. And I need to register it in apps, maybe?
I did this:
class MyAdminSite(admin.AdminSite):
def get_urls(self):
urls = super().get_urls()
my_urls = [
path('my-view', self.my_view, name='my-view')
]
return my_urls + urls
def my_view(self, request):
# do something ..
admin_site = MyAdminSite(name='my_admin')
and this in urls.py:
from .admin import admin_site
urlpatterns = [
path('admin/', admin_site.urls),
and this in unchanged in INSTALLED_APPS in settings:
'django.contrib.admin',
But, now it only shows the admin for the one app, instead of for all the apps. So how do I get it to auto-discover all the apps like it used to? Or is there a simpler way?
P.S. There is also a question on this, but the answer didn't have enough detail for me to use it.
EDIT: I'm reading Make new custom view at django admin and Django (1.10) override AdminSite ..
EDIT 2: This was a hack, but it works for me:
from django.contrib.admin import site
admin_site._registry.update(site._registry)
I had to update in the right place (the project urls.py), perhaps when all the other admin stuff is already loaded.

Use django-summernote without using form?

I am making a small site with Django. For some reasons, I am not using Django forms to render the forms in templates. I am doing the "traditional" way, by defining <input>, <textarea>, <select> etc in the template html files. I want to use django-summernote to enable rich text, especially image uploading, in a textarea. Is it possible to using Use django-summernote without using django.form? If so, how? I have added:
in settings.py:
MEDIA_ROOT = '/home/name/project/media/'
MEDIA_URL = '/media/'
in urls.py:
from django.conf.urls import include
urlpatterns = [
...
url(r'^summernote/', include('django_summernote.urls')),
...
]
As you are not using django forms, there is no need to use django-summernote, simply follow the instructions at the getting started page to integrate it with your template.

Is it possible to implement django login-authentication with out writing my own template?

my project's urls.py:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url('^', include('django.contrib.auth.urls')),
]
and, my app url's are:
from django.conf.urls import include, url
urlpatterns = [
url(r'^login/$', django.contrib.auth.views.login, {'template_name': 'login.html'}, name='login'),
url(r'^logout/$', django.contrib.auth.views.logout, {'template_name': 'logout.html'}, name='logout'),
]
I'm learning about django authentication. As after reading some docs, I've made a sample app and tried to implement the django authentication system on it.
Is there any built-in django template for these built in views. I've tried it without writing my own template but it throws exception. When I add my login template it works fine.
Is it possible to implement django-authentication with out writing my own 'login.html' template? Is django really having any built-in one? If it is, then how can I include it in my app?
Thanks! in advance
No, Django does not come with a login template. From the authentication docs:
It’s your responsibility to provide the html for the login template, called registration/login.html by default.
If Django came with a login template, it would probably look basic. Django doesn't know template structure, so wouldn't be able to inherit from your base template. So most people would end up creating a custom login template anyway. However, it would be easier for new users if Django included a template, however basic it looked.
Note that the authentication docs includes a sample template that you can use.
As an aside, you should either include 'django.contrib.auth.urls', or add entries for login and logout to your app's urls (this allows you to override the template name). You don't need to do both.

Is there a way to render a html page without view model?

Is there a way to render a html page without having a view model in django if a page is going to display only static html?
Also, can I redirect to a html page instead of a url? For example, instead of doing this:
return HttpResponseRedirect('form/success/')
can I do this:
return HttpResponseRedirect('success.html')
?
direct_to_template no longer works in Django 1.8. Here is how to do it in 1.8, in the urls.py:
from django.views.generic import TemplateView
urlpatterns = [
url(r'^$', TemplateView.as_view(template_name="your_static.html"), name='whatever'),
]
You can render a template without a view using the generic view direct_to_template.
Redirecting to success.html isn't the way to go in django since success.html doesn't have an associated URL. You always have to bind the template to an url via a view (direct_to_template is a view which gets all its arguments from the conf in urls.py, but it's still a view)
For static HTML you could use Flatpages app.
You could also serve rendered templates (which could contain only static HTML) but you will need a view:
from django.shortcuts import render_to_response
def some_view(request):
return render_to_response('sometemplate.html')
About redirection, basically you can't redirect to HTML page by just giving the filename, even if it were statically served by the web server you would still be using a URL that points to that file.
I'm pretty sure this isn't what OP wanted to do, but if someone wants to render something without a view, (for sending an email or appending to other html code), you can use this:
from django.template.loader import render_to_string
html = render_to_string('template.html', params)
Use lambda function.
urlpatterns = [
path('', lambda request: render(request, 'homepage.html'))
]