Use django-summernote without using form? - django

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.

Related

How to show flatpages with a single URL in Django?

I am trying to namespace Flatpages in Django. I included /pages/ in URLConf and added one URL /help/ in Django Admin Sites module. However, the page is loading with '/help/' and '/pages/help/' both URLs. I am trying to stop this behaviour and only load the page with /pages/help/. How is this possible?
urlpatterns = [
...
url(r'^pages/', include('django.contrib.flatpages.urls')),
]
You must have the fallback middleware installed in your MIDDLEWARE_CLASSES setting:
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware'
Remove it, and then it will only work on the prefix you have specified.

How to reverse match multiple Django admin sites (Custom admin site namespaces)

When you extend AdminSite to create another admin site how do you go about being able to reverse match each site? It seems the admin namespace is hardcoded reverse('admin:index'), is there a way to supply a custom namespace?
You may be confused with the namespace in django. If you are interested to clarify that confusion, you may read up the discussion here.
If you want to try solve your problem, there is a specific documentation for multiple admin sites.
Below are example solutions mostly copied from official documentation
Example Solution:
# urls.py
from django.conf.urls import url
from .sites import basic_site, advanced_site
urlpatterns = [
url(r'^basic-admin/', basic_site.urls),
url(r'^advanced-admin/', advanced_site.urls),
]
And
# sites.py
from django.contrib.admin import AdminSite
class MyAdminSite(AdminSite):
site_header = 'Monty Python administration'
basic_site = MyAdminSite(name='myadminbasic')
advanced_site = MyAdminSite(name='myadminadvanced')
Reversing
reverse('myadminbasic:index') # /basic-admin/
reverse('myadminadvanced:index') # /advanced-admin/

Django admin view uploaded photo

I have implemented photo upload in Django but I have a problem to view it Django admin.
models.py
class WorkPlacePhoto(models.Model):
file = models.FileField(storage=FileSystemStorage(location=settings.MEDIA_ROOT), upload_to='uploads')
Photos are saved in PATH_TO_APP/media/uploads/ and I can view them in my app. However, in admin panel, when I clicked on the link which admin app is generated it gives me 404 error as
Request Method: GET
Request URL: http://127.0.0.1/admin/wpphotos/workplacephoto/9/media/uploads/aosa_glb_ho_778_hi.jpg/
u'9/media/uploads/aosa_glb_ho_778_hi.jpg' object is not found
Although the message is clear, I couldn't figure out which url should be written to view the image and of course how admin class should be modified.
I glad to suggest me a way to achieve this. Thanks...
From you description I am guessing your MEDIA_URL isn't set correctly, something which is a bit tricky to do using the Django development web server.
I am guessing that the link's href would probably be media/uploads/aosa_glb_ho_778_hi.jpg where you probably want /media/uploads/aosa_glb_ho_778_hi.jpg so it is relative to http://127.0.0.1/ not to where you happen to be now http://127.0.0.1/admin/wpphotos/workplacephoto/9/.
See the static files documentation for inspiration of how to serve your images with the Django development server.
I have the same issue but I managed to fix it -but it's not a correct method and I'd like to use a better solution if possible.
What I did was, I have apache running on port 80, so I created a symbolic link in the /var/www folder which is pointing to the Images folder in my Django App directory. And the Media URL is basically this:
MEDIA_URL = 'http://127.0.0.1/Images/'
This work fine, but I don't like the solution.
I didn't quite follow the solution explained above. Could someone please explain more?
By now, you probably either solved your problem or quit it altogether, but after all these years people (like me) still have this problem. Here's how I just solved it:
On settings.py, I set not only STATIC_URL (with the name of the static folder) and STATIC_URL (with the path to that folder) but I also set MEDIA_URL and MEDIA_ROOT accordingly, where the uploaded images will be stored at;
On my model class, I have my picture field looking as follow:
picture = models.FileField(upload_to='images/')
It will upload the image to my MEDIA_ROOT folder, appending /images/ to it.
And finally, on my urls.py, my urlpatterns looks like this:
urlpatterns = [
url(r'^admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Notice how I appended the static right after the list.
Django's documentation makes it clear that this strategy is not recommended for production environment and it gives some alternatives you can choose from. For more information: https://docs.djangoproject.com/en/1.10/howto/static-files/#serving-files-uploaded-by-a-user-during-development
You probably have in your settings.py
MEDIA_URL = 'media/'
Being the url relative, you have that behavior in the admin panel. The solution would be to set the url as absolute
MEDIA_URL = '/media/'

Django: Views problem with Django + django-registration + jinja

So, I have a django project that is using jinja2 rendering, and I also installed django-registration to make my life easier. I ran into the following problem:
Going to homepage I render it with jinja. In order to check for authentication, I have to use jinja's syntax, which is user.is_authenticated(). However, in regular django templating, this check is done with user.is_authenticated. If in regular django templating there are (), it gives error.
So going to the /accounts/login/ page, the django-registration modul doesn't do anything special, so it forwards the url to the standard django views the following way:
from django.contrib.auth import views as auth_views
url(r'^login/$',
auth_views.login,
{'template_name': 'registration/login.html'},
name='auth_login'),
So I know for sure I shouldn't be changing the django.contrib.auth view, but then where do i put my own view? In myapp/views.py?
And also, do I have to copy paste the django view, and then modify on top of it (in this case simply replace the render with render_jinja) or is there a way to 'extend' this original django view to my own slightly modified view for logging in?
Whether right or wrong, in the registration module, I made a new view, that handled the logging, copying a few lines from here and there. It logical and seems to be working fine.

Django static page?

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