How to open the attached files from django admin? - django

can anyone help me in how to make the attched files opened from django admin website?
this is my code:
IDAttached = models.FileField(upload_to='/documents/%Y/%m/',null=True, blank= True)
Thanks in advance.

In order to see what the user of your django site uploads to the database you need to register your models.py of the app to the admin interface. For instance say you have models.py as below:
class ABC(models.Model):
ABCFILE=models.FileField(upload_to='<path>/%Y/%m/%d')
create an admin.py file inside your app folder and the following code:
from equipo.<your_app_name>.models import *
from django.contrib import admin
admin.site.register(ABC)
after this you be be able to see it on your admin interface.

If you have set MEDIA_URL correctly in your settings.py you should be able to get an address for those files at my_model.IDAttached.url
Django a runserver should display them for you in development but in production you'll need to configure your web server to display the contents of MEDIA_ROOT itself.

Related

Django superuser admin

I can't load the admin login screen.
have opened a Web browser and went to “/admin/” on local domain http://127.0.0.1:8000/admin/
was supposed to open admin login window.
First, check if your Django server is running.You can do that, by typing python manage.py runserver in the terminal and then visiting http://127.0.0.1:8000/admin/ in your browser.
Second, check if the Django admin is enabled. Open settings.py in your project folder and check if INSTALLED_APPS list has the django.contrib.admin element.
Third, check if you have an admin.py file in your app directory. The code should looks something like this:
from django.contrib import admin
from .models import MyModel
admin.site.register(MyModel)
Fourth, Check for your login credentials in your Django settings. Go to your project's settings.py file and you have to have this code:
LOGIN_URL = '/admin/login/'
LOGIN_REDIRECT_URL = '/admin/'
And the last option is to try clearing your browser cache and cookies and restarting the server.

Django Ckeditor Save incomplete data from richtextfield to cookies or server

The title, in my opinion, is self-explanatory. Basically let’s say a user if filling out an ckeditor richtextfield like so:
#app/models.py
from django.db import models
from ckeditor.fields import RichTextField
class App_Model(models.Model):
content = RichTextField()
Now let’s say an user left the page. The content will not be saved. I want the content to be saved, whether it be cookies or to the server, so the user can come back and pickup where he left off. How would I do this, preferably in the most simplest way possible.
Thanks.
One way to save the content is by using the CKEditor-AutoSave-Plugin
plugin for CKEditor 4, which saves the data in an HTML5 LocalStorage (client-side).
You just need to download and add that plugin to the django-ckeditor module.
Download and unpack the CKEditor-AutoSave-Plugin into your static directory. Make sure that you put the autosave folder from the archive into this directory:
<static-dir>/ckeditor/ckeditor/plugins/autosave
Where <static-dir> refers to the directory for your static files. This could be directly inside your app folder (next to admin.py, apps.py etc). For more details check the Django documentation.
Configure your CKEditor by adding this to your settings.py
CKEDITOR_CONFIGS = {
'default': {
'extra_plugins': ['autosave'], # Use the 'autosave' plugin
'autosave': { # Configuration on the autosave plugin
'autoLoad': True # Don't ask for confirmation to restore
}
},
}
For more configuration options check the documentation on GitHub.

Change header 'Django administration' text on nginx

I followed this question's answers to change my django admin panel title header.
I tried this:
There is an easy way to set admin site header - assign it to current
admin instance in urls.py like this
admin.site.site_header = 'My admin'
But it just works when I'm running the page via Python manage.py runserver
My question is how can I change the admin title header when I'm running the site via gunicorn and nginx
writing this code at the bottom of urls.py somehow worked:
admin.site.site_header = 'My admin'
If you already have an admin.py file started wherein you have registered your particular models, you can simply adjust these values there.
your_app/admin.py
# Simple admin setup
from django.contrib import admin
from .models import MyModel
# Register model
admin.site.register(MyModel)
# Tweak admin site settings like title, header, 'View Site' URL, etc
admin.site.site_title = 'My App Admin'
admin.site.site_header = 'My App Admin'
You can find all the attributes here.
follow the below steps to customize the site header and site title text of django admin login page :
1.)First import admin module in settings.py file like as below :
from django.contrib import admin
2.)In bottom of settings.py file add these lines:
admin.site.site_header = 'MY_SITE_HEADER'
admin.site.site_title = 'MY_SITE_TITLE'
The above method works in latest version of django i.e.1.11.3 till date.
You can make changes to parts of the admin by providing a template in an admin subdir of your templates directory to override what is provided by admin.
In this case, you'd want to provide a base_site.html template. You can see what the default one looks like here: https://github.com/django/django/blob/master/django/contrib/admin/templates/admin/base_site.html

Django 1.5.1 - Admin.py missing while running startapp

I've been following the DjangoProject tutorial. When I run python manage.py startapp newapp while in the same directory as manage.py. In the newapp directory I see init.py, models.py, tests.py, and views.py but not admin.py file. Where is admin.py?
I am running Django 1.5.1 in Windows 8
You have to create an admin.py file.
you don't necessarily need an admin.py file,
just import the admin module in your models.py file,
from django.contrib import admin
and for each model do the following:
admin.site.register(model1)
admin.site.register(model2)
However, this is not best practice, but since it's just a tutorial, it will work.
You also need to uncoment the relevant lines in the urls.py file
I think I had the same frustrations following the DjangoProject tutorial - however, when I cross-referenced it with with the DjangoBook tutorial (for the same version, I believe, 1.5.1), I found that an admin.py file was not necessarily created after a python manage.py startapp xyz command -- moreover, I also uncommented all of the admin options in urls.py, views.py, and settings.py - so a bit of a mix of what Neal and Ibrahim said
You have to create your own admin.py file in the app if you want it. Indeed, this file is optionnal and isn't created by startapp.
If you want a default template to begin your admin.py, it should be:
from django.contrib import admin
from models import Model1, Model2
class Model2Admin(admin.ModelAdmin):
list_display = ('title', 'content', 'date')
# Just an example, chekc docs and tutorials for more info.
admin.site.register(Model1)
admin.site.register(Model2, Model2Admin)
The reason there is no default admin.py is because you don't have any models yet when you create your new application; so there is nothing to add to the admin section.
Further, you may not want to admin all the models in your application; or you may be creating an application that does not need any admin hookups; or you may not be using the admin application at all in your project.
Since django cannot decide this for you, there is no default admin.py generated.
To create one, if you are following the tutorial - simply keep reading and in part two you'll create the admin.py file when you learn about the admin contrib app and how to integrate it with your custom models.

django inspectdb in admin site (admin.site.register)

I have been given a small legacy db (mysql) to work with, and therefore, I have tried to use the inspectdb command in django.
So, I use something like:
python manage.py inspectdb > models.py
It works fine, and I take the models.py and copy the class it created into my app (searchapp) as a models.py file.
I have done a syncdb, and everything went fine. Results are as expected.
Now, I have tried to add this model into my admin site, using:
from searchapp.models import Abaqus
from django.contrib import admin
admin.site.register(Abaqus)
stored in a file called admin.py (Abaqus is the name of the class generated by inspectdb and searchapp is the app name). However, I am not able to see the app on the admin site. I have checked my settings.py and it includes the 'searchapp'.
I was wondering if someone could point me in the right direction..
I suspect admin.py is not loaded. You could check this by putting a debug statement just above the register call:
import pdb;pdb.set_trace()
admin.site.register(Abaqus)
If this is in fact the case, the correct way to ensure admin.py is loaded is to call django.contrib.admin.autodiscover() at the beginning of your main url conf.
If you've written no admin classes and don't want an admin.py, you can call admin.site.register(Abaqus) right below where the model is defined, inside models.py.
If you have an admin module structured like the following, import admin within models.py to ensure the code is run:
myapp
|--models.py
|--views.py
|--admin
|--__init__.py
|--views.py
|--base.py
Another possible cause would be that you are missing the permissions to edit the model. Double check that you are logged in as a superuser.