[django]how can i change Settings.py? - django

Several configuration files exist.
If these files have different names
How do I change the settings file every time I run this command?
"python manage.py runserver"

Its so simple
read Main Django Tutorial, its all about setting django configuration
a shortcut for using in runserver command is --settings= and this also works with uwsgi
but if you intend to change setting without re-running the server django-constance is the answer

you can add to manage.py file
def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project_name.settings_name')
After that run
py manage.py ----

Related

Change API_BASEURL [duplicate]

I would like to make the default port that manage.py runserver listens on specifiable in an extraneous config.ini. Is there an easier fix than parsing sys.argv inside manage.py and inserting the configured port?
The goal is to run ./manage.py runserver without having to specify address and port every time but having it take the arguments from the config.ini.
create a bash script with the following:
#!/bin/bash
exec ./manage.py runserver 0.0.0.0:<your_port>
save it as runserver in the same dir as manage.py
chmod +x runserver
and run it as
./runserver
Actually the easiest way to change (only) port in development Django server is just like:
python manage.py runserver 7000
that should run development server on http://127.0.0.1:7000/
As of Django 1.9, the simplest solution I have found (based on Quentin Stafford-Fraser's solution) is to add a few lines to manage.py which dynamically modify the default port number before invoking the runserver command:
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings.dev")
import django
django.setup()
# Override default port for `runserver` command
from django.core.management.commands.runserver import Command as runserver
runserver.default_port = "8080"
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
All of the following commands are possible to change the port while running django:
python manage.py runserver 127.0.0.1:7000
python manage.py runserver 7000
python manage.py runserver 0:7000
Create a subclass of django.core.management.commands.runserver.Command and overwrite the default_port member. Save the file as a management command of your own, e.g. under <app-name>/management/commands/runserver.py:
from django.conf import settings
from django.core.management.commands import runserver
class Command(runserver.Command):
default_port = settings.RUNSERVER_PORT
I'm loading the default port form settings here (which in turn reads other configuration files), but you could just as well read it from some other file directly.
you can try to add an argument in manage.py like this
python manage.py runserver 0.0.0.0:5000
python manage.py runserver <your IP>:<port>
or you pass the port like this
python manage.py runserver 5000
python manage.py runserver <your port>
We created a new 'runserver' management command which is a thin wrapper around the standard one but changes the default port. Roughly, you create management/commands/runserver.py and put in something like this:
# Override the value of the constant coded into django...
import django.core.management.commands.runserver as runserver
runserver.DEFAULT_PORT="8001"
# ...print out a warning...
# (This gets output twice because runserver fires up two threads (one for autoreload).
# We're living with it for now :-)
import os
dir_path = os.path.splitext(os.path.relpath(__file__))[0]
python_path = dir_path.replace(os.sep, ".")
print "Using %s with default port %s" % (python_path, runserver.DEFAULT_PORT)
# ...and then just import its standard Command class.
# Then manage.py runserver behaves normally in all other regards.
from django.core.management.commands.runserver import Command
In Pycharm you can simply add the port to the parameters
first you apply the migrations for app
python manage.py migrate
then:
python manage.py runserver <your port>
after in browser run 127.0.0.1:(your port)
in the last version of Django(Right now: 4.0.3), you can add these lines to your settings.py file
from django.core.management.commands.runserver import Command as runserver
runserver.default_port = "8000"
in your project manage.py file add
from django.core.management.commands.runserver import Command as runserver
then in def main():
runserver.default_port = "8001"
I'm very late to the party here, but if you use an IDE like PyCharm, there's an option in 'Edit Configurations' under the 'Run' menu (Run > Edit Configurations) where you can specify a default port. This of course is relevant only if you are debugging/testing through PyCharm.
Create enviroment variable in your .bashrc
export RUNSERVER_PORT=8010
Create alias
alias runserver='django-admin runserver $RUNSERVER_PORT'
Im using zsh and virtualenvs wrapper. I put export in projects postactivate script and asign port for every project.
workon someproject
runserver
If you wish to change the default configurations then follow this steps:
Open terminal type command
$ /usr/local/lib/python<2/3>.x/dist-packages/django/core/management/commands
Now open runserver.py file in nano editor as superuser
$ sudo nano runserver.py
find the 'default_port' variable then you will see the default port no is '8000'. Now you can change it to whatever you want.
Now exit and save the file using "CTRL + X and Y to save the file"
Note: Replace <2/3>.x with your usable version of python
For Django 3.x, just change default_port in settings.py. Like this:
from decouple import config
import django.core.management.commands.runserver as runserver
runserver.Command.default_port = config('WebServer_Port', default = "8088")
Then, if you want to specify the port, just add a new line in your setting.ini
[settings]
WebServer_Port=8091
If not, delete this parameter.
This is an old post but for those who are interested:
If you want to change the default port number so when you run the "runserver" command you start with your preferred port do this:
Find your python installation. (you can have multiple pythons installed and you can have your virtual environment version as well so make sure you find the right one)
Inside the python folder locate the site-packages folder. Inside that you will find your django installation
Open the django folder-> core -> management -> commands
Inside the commands folder open up the runserver.py script with a text editor
Find the DEFAULT_PORT field. it is equal to 8000 by default. Change it to whatever you like
DEFAULT_PORT = "8080"
Restart your server: python manage.py runserver and see that it uses your set port number
It works with python 2.7 but it should work with newer versions of python as well. Good luck
I was struggling with the same problem and found one solution. I guess it can help you.
when you run python manage.py runserver, it will take 127.0.0.1 as default ip address and 8000 as default port number which can be configured in your python environment.
In your python setting, go to <your python env>\Lib\site-packages\django\core\management\commands\runserver.py and set
1. default_port = '<your_port>'
2. find this under def handle and set
if not options.get('addrport'):
self.addr = '0.0.0.0'
self.port = self.default_port
Now if you run "python manage.py runserver" it will run by default on "0.0.0.0:
Enjoy coding .....

How I can run a telegram bot with the Django project?

Now, to run the bot through Django, I first use the python manage.py runserver command and then follow the link to launch a view with my bot. Can you tell me if there is an easier way to start my bot automatically when starting a Django project?
Actually, you can use a management command to run your bot with something like
python manage.py runbot
All Django context, including DB and settings, will be available
Reference to management command page:
https://simpleisbetterthancomplex.com/tutorial/2018/08/27/how-to-create-custom-django-management-commands.html
Maybe is a little late but you can do the following:
create the bot.py file in the same folder where manage.py is.
inside the bot.py make sure you import the following:
import django
import os
os.environ['DJANGO_SETTINGS_MODULE'] = '{Folder where your settings are}.settings'
django.setup()
and in order to run you just type python bot.py

Understanding the difference between django-admin runserver and mange.py runserver

What is the basic difference between the django.admin and manage.py?
Also, What is the meaning of the sentence, "manage.py acts as a "thin wrapper
around django.py" ?
here is the documentation doc that outlines the difference between django-admin and manage.py:
django-admin.py is Django’s command-line utility for administrative tasks. This document outlines all it can do.
In addition, manage.py is automatically created in each Django
project. manage.py is a thin wrapper around django-admin.py that takes
care of two things for you before delegating to django-admin.py:
It puts your project’s package on sys.path.
It sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your project’s settings.py file.
Here's more documentation explaining the difference:
https://django.readthedocs.org/en/1.4/ref/django-admin.html
https://docs.djangoproject.com/en/1.8/ref/django-admin/
To add what I said on the comments above - manage.py is needed because every app name will be different and you have to specify your app's settings to run your django app successfully. There's really no difference between django-admin and manage.py except this one line
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "[App name].settings")
everything that you type after manage.py, django-admin.py takes care of as shown:
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
Sometimes, it helps just looking at the source code to see what is really going on if the documentation doesn't help.

Modifing settings in Django

I'm trying to get Django's manage.py to run with modified settings files.
I have three settings files
settings.py
preview.py
live.py
settings.py contains my base settings but on live & preview sites I point the ['DJANGO_SETTINGS_MODULE'] to live or preview which in turn load the base file and any settings specific to that environment.
This works fine, until I try to run migrations using manage.py
I updated the manage.py file to import settings.preview but now when I try to run syncdb or migrate I get this error:
django.core.exceptions.ImproperlyConfigured:
You haven't set the DATABASE_ENGINE
setting yet.
My settings file has this in it though:
DATABASE_ENGINE = 'mysql'
How can I get this to work?
Don't modify manage.py if you can help it. Instead pass it the --settings argument to choose an alternate settings module. Setting up a shell script or alias will make it easier to use this.

Unable to start a django server in my computer

I exported the path of my django project by
$ export DJANGO_SETTINGS_MODULE=/Users/masi/Documents/Test/djangobook/ch3.settings
I run unsuccessfully
$ django-admin.py runserver
Error: Could not import settings '/Users/masi/Documents/Test/djangobook/ch3.settings' (Is it on sys.path? Does it have syntax errors?): Import by filename is not supported.
How can you start Django server without the error message?
Your $DJANGO_SETTINGS_MODULE should just be set to ch3.settings. Just make sure that the ch3 app is in your $PYTHONPATH, too.
For example, if your app is at /Users/masi/Documents/Test/djangobook/, then set $DJANGO_SETTINGS_MODULE to ch3.settings, and make sure your $PYTHONPATH includes /Users/masi/Documents/Test/djangobook.
$ export PYTHONPATH=/Users/masi/Documents/Test/djangobook/
$ export DJANGO_SETTINGS_MODULE=ch3.settings
From the django docs on django-admin.py and manage.py:
django-admin.py is Django’s command-line utility for administrative tasks.
In addition, manage.py is automatically created in each Django project. manage.py is a thin wrapper around django-admin.py that takes care of two things for you before delegating to django-admin.py:
It puts your project’s package on sys.path.
It sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your project’s settings.py file.
Generally, when working on a single Django project, it’s easier to use manage.py
So, if your directory structure looks like:
djangobook/
ch3/
settings.py
Do the following and you can ignore all DJANGO environment variables (unless you have some really weird install):
$ cd /Users/masi/Documents/Test/djangobook/ch3/
$ python manage.py runserver
For those that come across the same error, when trying to run something similar:
python manage.py runserver --settings=settings_dev
When the settings file is within an app directory, like so:
mysite/
settings.py
settings_dev.py
requirements.txt
manage.py
You don't have to specify $PYTHONPATH (at least not four years on) you just need to make sure your --settings value contains the folder name — you also need to use dot notation, slashes will not do.
python manage.py runserver --settings=mysite.settings_dev
It is the same story when exporting a $DJANGO_SETTINGS_MODULE value:
export DJANGO_SETTINGS_MODULE=mysite.settings_dev
Might save someone else the time that I lost working that out.
You can also try manage.py.
From your project directory, run
$ python manage.py runserver
Even though it's just a wrapper, manage.py always works for me while django-admin.py doesn't. Obviously we're both doing something wrong (I just got started with Django), but this should get you going at least.