Django REST API Generics displaying improperly - django

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)

Related

Swagger Django [No operations defined in spec]

I have been trying to integrate Swagger with Django Application and encounter this error:
No operations defined in spec!
My project structure is
App
views.py
urls.py
..
App2
settings.py
urls.py
..
I am using drf_yasg for my purpose. I have included all details in settings.py and in App2 I have this in urls.py:
from rest_framework import permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
schema_view = get_schema_view(
openapi.Info(
title="Testing",
default_version='v1',
description="Doc Integration",
terms_of_service="https://www.google.com/policies/terms/",
contact=openapi.Contact(email="abc#abc.com"),
license=openapi.License(name="BSD License"),
),
public=True,
permission_classes=(permissions.AllowAny,),
)
urlpatterns=[path("admin/", admin.site.urls),
path("", include("app.urls")),
path('swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
path('redoc', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),]
My class views such as (class Att(generic.TemplateView) and methods are declared in views.py under App. I tried methods such as #swagger_auto_schema, #api_view, to display app class and functions in the documentation. But it just returns No operations defined in spec!.
I tried using routers to register the view as well but did not work, even django-rest-swagger fails. Any help is appreciated. Thanks!
I have a project made of Django and I'm new to DRF. I had seen No operations defined in spec! as well while trying drf-yasg which is one of Swagger generators. Hope this way works for you.
I imported ->
from rest_framework.views import APIView
The class in views.py was like
class FeedView(View):
and the functions showed up when I changed the class like
class FeedView(APIView):
I need to deal with #swagger_auto_schema, #api_view to make my API document works with proper parameters.

How to reverse path for custom user when testing Django Rest Framework

I'm building a Django 3 app using a Custom User. I'm trying to test the Custom User, but I can't get the url using reverse. I'm using Django Rest Framework. I'm using Django Rest Auth.
myapp.api.urls.py:
app_name = 'myApp'
from django.urls import include, path, re_path
urlpatterns = [
...
path('users/', include('users.urls'), name='UsersURLS'),
]
myapp.users.urls.py
from django.urls import include, path
from . import api
app_name = 'users' # namespace for reverse
urlpatterns = [
path('', api.UserListView.as_view(), name='UsersPath'),
]
myapp.users.api.py
class UserListView(generics.ListCreateAPIView):
queryset = models.CustomUser.objects.all()
serializer_class = serializers.UserSerializer
authentication_classes = (TokenAuthentication,)
And in test_users_api.py:
from users.api import UserListView
user_detail_url = reverse('myApp:UsersURLS-list')
...
def test_user_detail(self):
self.client.force_authenticate(user=self.user)
response = self.client.post(user_detail_url, {'pk': self.user.id }, format='json')
Whatever I try for the reverse, I get an error that is it not a valid view or function name. So far I have tried:
reverse('myApp:UsersURLS-list')
reverse('users:UsersPath-list')
reverse(UserListView)
reverse('UsersPath')
I'd be grateful for any idea what I'm doing wrong, and how to get the reverse URL. I have it working for the rest of my endpoints which use router, but I can't see what's needed for my Custom User, which I set up following a tutorial and it works fine otherwise.
I found an answer in the rest-auth source code, at venv36/lib/python3.6/site-packages/rest_auth/tests/test_api.py, and mixins.py in the same folder. I don't know why my attempts to reverse fail, but this works:
user_detail_url = reverse('rest_user_details')
All names for reverse can be found in the mixins.py file.

Django Caching on front-end

I was working with 2 applications that are within a DJango project: "customer" and "vendors". Each application has a HTML file named "testindex.html".
Whenever I typed:
http://myhost/customer/basic_info
the correct page would show up
If I typed
http://myhost/vendors/basic_info
the page from http://myhost/customer/basic_info would show up
I found out that it was due to caching (since both applications use "testindex.html"). So again, "testindex.html" is caching.
How can one get around this problem?
TIA
Details are listed below. I have the following views defined:
urls.py for the project
urlpatterns = [
... snip ...
url(r'^customer/', include('libmstr.customer.urls')),
url(r'^vendors/', include('libmstr.vendors.urls')),
]
views.py for customer
from django.shortcuts import render
def basic_info(request):
return render(request, 'testindex.html', {})
views.py for vendors
from django.shortcuts import render
def basic_info(request):
return render(request, 'testindex.html', {})
urls.py for customers
from django.conf.urls import url
from . import views
# list of templates
app_name = 'customer'
urlpatterns = [
url(r'^basic_info/$', views.basic_info, name='basic_info'),
]
urls.py for vendors
from django.conf.urls import url
from . import views
# list of templates
app_name = 'vendors'
urlpatterns = [
url(r'^basic_info/$', views.basic_info, name='basic_info'),
]
It sounds like you have two templates, customers/templates/testindex.html and vendors/templates/testindex.html.
When you call render(request, 'testindex.html', {}), the app directories template loader searches the templates directory for each app in INSTALLED_APPS, and stops the first time it finds a match. If customers is above vendors in INSTALLED_APPS, then it will always use the customers template.
For this reason, Django recommends that you name your templates customers/templates/customers/testindex.html and vendors/templates/vendors/testindex.html, and change your views to use customers/testindex.html and vendors/testindex.html. This way you avoid clashes.

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?

grappelli dashboard ValueError on two admin sites

I want use two admin sites for my project. Each with grappelli dashboard. I've executed this commands:
python manage.py customdashboard dashboard.py
python manage.py customdashboard dashboard.py
twice (once in project/project and second time in project/app)
#file system
project
project
dashboard.py
urls.py
app
dashboard.py
admin.py
#settings.py
GRAPPELLI_INDEX_DASHBOARD = {
'django.contrib.admin.site': 'project.dashboard.CustomIndexDashboard',
'app.admin.operator_site': 'app.dashboard.CustomIndexDashboard',
}
#urls.py
from django.conf.urls import patterns, url, include
from django.contrib import admin
from app.admin import admin_site
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^myadmin/', include(operator_site.urls)),
url(r'^grappelli/', include('grappelli.urls')),
)
#app/admin.py
from django.contrib.admin import AdminSite
class MyAdminSite(AdminSite):
pass
admin_site = MyAdminSite()
The problem is when I go to /admin/ everything is ok, but when I go to /myadmin/ , I've got ValueError
Dashboard matching "{'app.admin.operator_site': 'app.dashboard.CustomIndexDashboard', 'django.contrib.admin.site': 'project.dashboard.CustomIndexDashboard'}" not found
full error trace: http://pastebin.com/w8W2eRPd
Where is the problem?
Ok, i've found it out. When making a subclass of a AdminSite on make an instance
admin_site = MyAdminSite()
you should use a custom name parameter (not 'admin'):
admin_site = MyAdminSite(name='myadmin')