How to run Django 1.6 LiveServerTestCase without HTTPS? - python

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.

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"]

Why swagger documentation doesn't work on PythonAnywhere domain?

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

problems setting up django-toolbar: the bar is not shown

I am trying to set up django-toolbar in my production site, so just certain IPs can use it. I have followed the official documentation (install and quick setup), but I can't see the toolbar.
The app is installed:
$ yolk -l | grep toolbar
django-debug-toolbar - 0.11.0 - active
I have added the required 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.sitemaps',
'modeltranslation',
'djangocms_text_ckeditor',
'cms',
'mptt',
'menus',
'south',
'sekizai',
'debug_toolbar',
# more stuff...
)
INTERNAL_IPS = ('---.---.---.---',) # my current ip, taken from the server logs so I'm sure it's correct
STATIC_URL = '/static/'
DEBUG = True
There's no error message. I have also run collectstatic just in case. No success.
Any suggestion? Thanks :-)
EDIT: it seems that the order within INSTALLED_APPS matters. Moving 'debug_toolbar' right after all 'django.contrib.*' and before 'modeltranslation' and the others solved the issue.
Have you set the INTERNAL_IPS to what is in request.META['REMOTE_ADDR'] (second answer here)?
I had a similar problem, the toolbar wasn't displaying and I couldn't figure out why. But when I printed request.META['REMOTE_ADDR'] my IP wasn't what I thought it was! (because I was using port forwarding and virtual box).
I found out the reason. Within INSTALLED_APPS, 'debug_toolbar' must appear as soon as possible. I had placed some other applications before ('modeltranslation', 'cms', etc.). Placing it just after all 'django.contrib.*' and before the others works fine.

Django: Error: Unknown command: 'makemigrations'

I am trying to follow the Django tutorial and I faced the following error when I enter python manage.py makemigrations polls
Unknown command: 'makemigrations'
Here's the link to the tutorial and I accomplished all the previous steps successfully and I am not sure what's going wrong now or how to fix it.
P.S.: I have already included "polls" in the INSTALLED_APPS!
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls',
'South',
)
Answer: I had to modify INSTALLED_APPS to :
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls',
)
and also used this command: python manage.py syncdb
Migrations were first added in version 1.7, officially released on September 2, 2014. You need to make sure your tutorial matches the version of Django you're working with. For instance, this version of the tutorial covers 1.9:
https://docs.djangoproject.com/en/1.9/intro/tutorial01/
Or, if you're using an older version of Django, you can change the "1.9" in that URL to whatever version you're on (back to 1.3). Or use the dropdown on the docs page to pick the version and search for "tutorial".
Find out what version of django you're running (thanks #BradyEmerson):
python -c "import django; print(django.get_version())"
If older than 1.8:
pip install --upgrade django
I was using version 1.9 and still getting this error. I had unapplied migrations and that was the root cause in my case. I ran 'python manage.py migrate' to apply them and it worked for me.
In django makemigration added after 1.7 so if you are using older version of Django then you have to change settings.py and add your application in installed app like
INSTALLED_APPS = (
'Demo',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
and then you can run command
python manage.py syncdb
You need to load the virtual environment before doing it.
Use below code for Linux/OSX:
source venv/bin/active
And the following code for Windows
source venv/Scripts/activate
I did following (for python version 3.6.4) to get this issue resolved:
install virtualenv
Activate virtualenv
Cheers
This worked for me: using ubuntu 20.04, Django 3.2
First after creating the model and addind to the setting, I did:
python manage.py makemigrations
I obtained the migration number here.
After that I did:
python manage.py sqlmigrate [NAME_OF_MODEL] [NUMBER_OF_THE_MIGRATION]
Finally:
python manage.py migrate
First time I add following piece of code into project_name\settings.py file.
`INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
#Django REST Framework
'rest_framework',
#Games application
'games.apps.GamesConfig',
]`
After save it, when run following code I got error.
`python manage.py makemigrations games`
Then I check the settings.py file I realize that there are two INSTALLED_APPS and second one has not followings. When I added these the code worked.
`#Django REST Framework
'rest_framework',
#Games application
'games.apps.GamesConfig',`

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