Why swagger documentation doesn't work on PythonAnywhere domain? - python

I have used swagger to document my APIs on the Django rest framework, and deploy it on Pythonanywhere.
but the documentation URL doesn't have any response.
it is my urls.py code for swagger:
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
schema_view = get_schema_view(
openapi.Info(
title="BoardGame API",
default_version='v1',
description="Test description",
terms_of_service="http://****.pythonanywhere.com/",
contact=openapi.Contact(email="contact#boardgame.local"),
license=openapi.License(name="TEST License"),
),
public=True,
permission_classes=(permissions.AllowAny,),
)
urlpatterns = [
...
path('', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
]
and my settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'drf_yasg',
'rest_framework_swagger',
'rest_framework_simplejwt',
'django_extensions',
...
]

Its because your static files has not been loaded on server try to collectstatic files and check in inspect of page whether css is loading or not

You have to learn about static in Django
When you apply it all UI related files will be created in a static folder
Then swagger UI will work on pythonanywhere

It's seems that static file is not loaded on live server:
step 1:
LOCAL= False
step 2:
python manage.py collectstatic
step 3:
Reset server
nginx: sudo systemctl restart nginx
gunicorn: sudo systemctl restart gunicorn

Related

Django Channels is not taking over deployment server

I am attempting Django Channels for the first time. I am following this tutorial - https://www.youtube.com/watch?v=cw8-KFVXpTE&t=21s - which basically explains Channels basics. I installed Channels in my virual environment using pip install channels and it installed the latest version, which is 4.0.0. I have laid out the basic setup. But when I run python manage.py runserver, I get -
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
January 10, 2023 - 02:37:40
Django version 4.1.3, using settings 'BrokerBackEnd.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
when I shoul be getting this -
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
January 10, 2023 - 02:37:40
Django version 4.1.3, using settings 'BrokerBackEnd.settings'
Starting ASGI/Channels version 4.0.0 development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
I cannot figure out what I might be doing wrong here and I cannot find any relevant solution anywhere. I would really appreciate any suggestions anyone might have.
settings.py
INSTALLED_APPS = [
'channels',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'corsheaders',
'rest_framework',
'Orders',
]
ASGI_APPLICATION = "BrokerBackEnd.asgi.application"
asgi.py
import os
from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter, URLRouter
import Orders.routing as route
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'BrokerBackEnd.settings')
application = ProtocolTypeRouter({
'http': get_asgi_application(),
'websocket': URLRouter(
route.websocket_urlpatterns
)
})
consumer.py
class OrderConsumer(WebsocketConsumer):
def connect(self):
self.accept()
def receive(self, text_data=None, bytes_data=None):
self.send(text_data="Message = " + text_data)
def disconnect(self, code):
return super().disconnect(code)
routing.py
websocket_urlpatterns = [
re_path(r"socket/order/", OrderConsumer.as_asgi())
]
As of django-channels 4.0.0 the daphne server was decoupled from the rest of channels. Now, you need to include daphne in your settings.py INSTALLED_APPS if you wish to use it. Include this at the top of your installed apps. The tutorial you are looking at uses an older version of channels.
INSTALLED_APPS = [
'daphne',
'channels',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'corsheaders',
'rest_framework',
'Orders',
]
ASGI_APPLICATION = "BrokerBackEnd.asgi.application"
Also, make sure daphne is actually installed:
pip install -U channels["daphne"]

django listing files instead of showing home page

I'm not able to figure out what's going wrong. I'm using pycharm and bitnami django stack for developing my first web application.
Here is my directory structure:
project name: myapp
location: C:\Bitnami\djangostack-1.7.8-0\apache2\htdocs\myapp
Directory structure:
myapp
manage.py
app
admin.py
models.py
settings.py
tests.py
urls.py
views.py
wsgi.py
migrations
templates
home.html
my settings.py has following data:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app',
)
ROOT_URLCONF = 'app.urls'
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)
my urls.py has following data:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'myapp/', views.homepage, name='home'),
]
my views.py has following data:
from django.http import HttpResponse
def homepage(request):
return HttpResponse("Hello, world!")
Now when I try to run:
http://localhost/myapp
It simply displays the list of files in the myapp directory
I'm not able to find why it is not executing from urls.py
If you want to get your app up and running with Apache, you will have to enable reverse-proxying.
To do this, you can try adding the following lines to /opt/bitnami/apache2/conf/httpd.conf, supposing that your application myapp is running at port 8000:
ProxyPass /myapp http://localhost:8000/myapp
ProxyPassReverse /myapp http://localhost:8000/myapp
To make sure that the mod_proxy module is enabled, please find the following lines and uncomment them:
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
Finally, after saving the changes you can proceed to restart Apache:
/opt/bitnami/ctlscript.sh restart apache
You should now be able to access your application in: http://localhost/myapp
I'm able to run the project with the development server. In Pycharm, I went to Tools -> Run manage.py Task -> run server
And then executed the url:
http://localhost:8000/myapp/
But still trying to figure out why it is not running with apache in Bitnami stack

All of the static (assets) not found on heroku

I've deployed a Django 1.8 on heroku and none of the static files getting found at all. I can't figure out what I'm doing wrong:
# settings.py
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app123'
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
APPEND_SLASH = False
I put all the static files into myproject/app123/static/(css|images|js) on localhost. When I run heroku run python manage.py collectstatic it doesn't copy anything:
$ heroku run python manage.py collectstatic
Running `python manage.py collectstatic` attached to terminal... up, run.7135
You have requested to collect static files at the destination
location as specified in your settings:
/app123/staticfiles
This will overwrite existing files!
Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel: yes
0 static files copied to '/app123/staticfiles', 117 unmodified.

How to run Django 1.6 LiveServerTestCase without HTTPS?

I have a Django 1.6 project in which I'm trying to run LiveServerTestCase. However, I don't need HTTPS for my test case, so I'd like to disable it. Is there a way to do this?
I run:
$ website/manage.py test website/tests.py:MySeleniumTests
and Firefox opens pointed at:
https://localhost:8081/
I am able to run a secure server on port 8081 with:
$ website/manage.py runserver_plus --cert cert 0.0.0.0:8081
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'django.contrib.admindocs',
'django.contrib.messages',
'django.contrib.staticfiles',
'south',
'website.nucleus',
'website.blog',
'django_extensions',
'debug_toolbar',
'django_nose',
'django_coverage',
'tinymce',
'django_ztask',
'timezones',
'tracking',
'gunicorn',
'raven.contrib.django.raven_compat',
'social.apps.django_app.default',
'report_builder',
'bootstrapform',
'bootstrap3',
)
By default, Django uses http:// protocol for live server tests, quote from the source code:
class LiveServerTestCase(TransactionTestCase):
static_handler = _StaticFilesHandler
#property
def live_server_url(self):
return 'http://%s:%s' % (
self.server_thread.host, self.server_thread.port)
Since, you see https:// protocol used in tests, something is redirecting your requests to https. Check your MIDDLEWARE_CLASSES setting.
For example, django-ssl-redirect middleware could be the culprit.

No module named pages.urls

I'm trying to install Django Gerbi-CMS and getting no luck. I've got through the installation a couple of times and not getting anywhere. When I run the server I get the error:
No module named pages.urls
When I install it tells me that it's installed correctly. However I look in the python2.7/site-packages and pages is not in there.
I installed on an existing site a while ago and I can't see what different other than pages and the other depencencies not showing in the above directory.
The last command I ran to install the package was:
$ sudo pip install django-page-cms
which like I said installed but nothing showing up in my site-packages.
Link to site - http://packages.python.org/django-page-cms/installation.html
SETTINGS:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.admindocs',
#'authority',
'pages',
'south',
'blog',
)
URLs:
if settings.DEBUG:
urlpatterns += patterns('',
# Trick for Django to support static files (security hole: only for Dev environement! remove this on Prod!!!)
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
url(r'^admin_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.ADMIN_MEDIA_ROOT}),
)
urlpatterns += patterns('',
(r'^', include('pages.urls')),
)
If you need any other info please let me know.
Thanks!
After going through the installtion process several times I worked out that because I was using 'sudo' it was installing on the server and not inside the virtualenv!
So when you install the app you need to Install it without sudo, otherwise it will install outside of the virtualenv!
Did you included the application in your settings.py file in the section called
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.admin',
'django.contrib.sites',
'mptt',
'tagging',
'pages', # You have to include it here
)

Categories

Resources