Django Upload/Download File - django

I am new to django. In my project I need to add user forms to upload some files and download the uploaded files.
Consider this as an online storage space.
This needs to be handled by class based views so that expansion of the views will be easy.
As of now I tried django-downloadview for objectdownloadview
https://django-downloadview.readthedocs.io/en/1.9/views/object.html
This works quite fine with the example. But I am not able to expand it for class based views for my project.
More over example is with slug I tried changing it to PK but its throwing errors.
I tried below code from django-downloadview example:-
Views.py
from django_downloadview import ObjectDownloadView
from .models import Document
default_file_view = ObjectDownloadView.as_view(model=Document)
Models.py
from django.db import models
class Document(models.Model):
slug = models.SlugField()
file = models.FileField(upload_to='object')
urls.py
from django.conf.urls import url
from .views import *
urlpatterns = [
url(r'^object/(?P<slug>[a-zA-Z0-9_-]+)/$',default_file_view,name='object'),
]
Can someone please help me here Or share a working example of simple 1 page django file upload and download stuff.
Regards,
Jaiswal

Related

I try to be able to load an image in the django admin, but I can't see it when I click

I don't know how to solve it. It's really frustrating. If you managed to help, I would be very grateful.
settings:
STATIC_URL = '/static/'
MEDIA_URL='Proyectoweb/media/'
MEDIA_ROOT=BASE_DIR / 'Proyecto/media'
url:
from django.urls import path
from ProyectoApp import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns+=static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
models:
from django.db import models
# Create your models here.
class Servicio(models.Model):
titulo=models.CharField(max_length=50)
contenido=models.CharField(max_length=50)
imagen=models.ImageField(upload_to='servicios')
created=models.DateTimeField(auto_now_add=True)
updated=models.DateTimeField(auto_now_add=True)
class Meta:
verbose_name='servicio'
verbose_name_plural='servicios'
def __str__(self):
return self.titulo
admin:
from django.contrib import admin
from .models import Servicio
# Register your models here.
class ServicioAdmin(admin.ModelAdmin):
readonly_fields=('created', 'updated')
admin.site.register(Servicio, ServicioAdmin)
For some reason, this page asks me for more details, when I think I have already given enough.
I try to be able to load an image in the django admin, but I can't see it when I click....

Django unable to load model into views

I am trying to import my models into views.py but I am unable to do so. However I am able to register them on the admin site but when I use the same code I used in admin.py to import the models into views.py, I get an error. I am using djongo so I am not sure if that changes anything about how to import them and I cannot seem to find the documentation for it.
models.py
from djongo import models
class Round(models.Model):
round_num = models.IntegerField(default=0)
admin.py
from django.contrib import admin
from .models import Round
admin.site.register(Round)
views.py
from .models import Round
When I try and run my views.py file I get the following error: ModuleNotFoundError: No module named 'main.models'; 'main' is not a package
Also my views, admin, and models file are all in the same directory. I have made the migrations and I can see my Round model in MongoDB. The only thing I cannot do is import it to the view
You need to have an __init__.py file in your directory. It should be inside of your main folder and at the same level as your views.py and models.py
As a workaround, since the models properly migrate to MongoDB. Using pymongo I have just connected to Mongo and have rendered data into my views this way. It works fine so if anybody else has an issue loading in their models, you can always just connect directly to the DB.

Django REST API Generics displaying improperly

Currently using a REST API and the generic views CreateUpdateDestroy, and my admin display GUI looks like this :
All the sources online that I've followed, tutorials etc get a generic view which looks much nicer.
Here is my views.py:
from rest_framework import generics
from models import Results
from serializers import ResulutsSerializer
class ResultsList(generics.ListAPIView):
queryset = Results.objects.all()
serializer_class = ResultsSerializer
class ResultsDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Results.objects.all()
serializer_class = ResultsSerializer
and urls.py:
from django.urls import path
from main import views
urlpatterns = [
path('results/', views.ResultsList.as_view()),
path('<int:pk>/', views.ResultsDetails.as_view())
]
what am I doing wrong?
It looks like you need to collect your app assets:
$ python manage.py collectstatic
# You can provide option: --settings=<your-settings-file> if you're using custom settings which is not default in manage.py
You will need to configure staticfiles settings in your Django settings module if not already configured – e.g. settings.py. Please follow documentation at:
https://docs.djangoproject.com/en/2.0/howto/static-files/
https://docs.djangoproject.com/en/2.0/ref/contrib/staticfiles/
If you are developing locally:
You should set DEBUG=True in your Django Settings Module (i.e. normally settings.py)

enable a model on Django Admin on Dreamhost

Hi I've created a new app (Paginas) but I cannot see inside the admin page. I've added 'paginas' to INSTALLED_APP. My admin.py is
# coding=utf-8
from django.contrib import admin
from .models import Pagina
class PaginaAdmin(admin.ModelAdmin):
prepopulated_fields = {"slug": ("titulo",)}
list_display = ('titulo' , 'contenido')
class Media:
js = ('/layout/grappelli/tinymce/jscripts/tiny_mce/tiny_mce.js','/layout/grappelli/tinymce_setup/tinymce_setup.js')
admin.site.register(Pagina,PaginaAdmin)
I am using passenger_wsgi.py file, I've touched tmp/restart.txt file, I've killed python process
I don't know what else can I do
The project you can see it on github https://github.com/totechess/paralisis_cerebral
What kind of error do you get when you go to your site yoursite.com/admin/
Did you add admin to INSTALLED_APPS? Note the S
....
'django.contrib.admin',
....
Perhaps your urls.py are not updated properly? What is the error?

Django: admin.py doesn't exist?

In DjangoBook, it says that the every books app should have its own admin.py. However, none of my apps have there own separate admin.py as the text suggests. I was just wondering if this is a Django 1.3 thing and if so, where is the admin.py data stored now, if not in a separate admin.py file?
The chapter I'm referring to is here:
http://djangobook.com/en/2.0/chapter06/
P.S. I'm not talking about django-admin.py
By default there is no admin.py file created for you when you create a new app, you will need to create your own. Here are the directions on how to create the admin.py file.
http://docs.djangoproject.com/en/1.3/ref/contrib/admin/
Edit: 1.3 is no longer supported, here is a link to 1.8:
https://docs.djangoproject.com/en/1.8/ref/contrib/admin/
You need to create admin.py in new app and edit it with from django.contrib import adminIn project/urls.py, in my case (mysite/urls.py) specify the url of newapp/admin (polls/admin) likefrom django.conf.urls import patterns, include, url from django.contrib import admin
admin.autodiscover() urlpatterns = patterns('',url(r'^admin/', include(admin.site.urls)),)