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
Related
In my django project i have to check if a database/table exist before starting application, i don't know how is better insert the code for check.
I try to add in views.py into login function a try except block but i was try to find an elegant and more effective solution.
Thanks in advance
To check before the app starts then you can use the AppConfig.ready() function. This gets called before the application starts, but after your project has started. If you want to check before the project starts then you will have to hook into the method you use to start your project, eg within wsgi.py or even manage.py
AppConfig.ready() docs Note that the docs specifically say
avoid interacting with the database in your ready() implementation.
But your use case may justify doing this.
The ready function is called when you run the commands from manage.py eg
manage.py shell / manage.py migrate / etc
It won't get called when your site is visited of course. If you want to run a DB check in response to a visitor action then that code should go into your view
You put the ready() function in your apps.py:
from django.apps import AppConfig
from django.db import connection
class MyAppConfig(AppConfig):
name = 'MyApp'
def ready(self):
print("i am the ready function and the database test code goes here")
# put your test code here, eg you could read all the tables from sqlite
with connection.cursor() as cursor:
cursor.execute("SELECT name FROM sqlite_master;")
rows=cursor.fetchall()
print (rows)
You can write your custom django admin commands and run it by using python manage.py your_command right before calling python manage.py runserver. There are detailed examples at the official documentation.
One of the advantages of using commands is that testing commands are fairly easy. Django has a package for calling commands django.core.management.call_command which enables funtionally test your command.
I am trying to run a script using django's runscript. I followed everything in the documentation. Did i miss something?
But when i tried running it from the command line. it says unknown command 'runscript'
(env) C:\Users\MIS\hr system\hr_project>python manage.py runscript automail.py
Unknown command: 'runscript'
You need to install django-extensions if you want the runscript command. If you don't want to do that, you can:
Run your script directly. Keep in mind that you need to specify the Django settings module as such:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project_name.settings")
from your_project.models import SomeModel
# Your code goes here...
Make a custom manage.py command. You can use the official how-to: https://docs.djangoproject.com/en/2.2/howto/custom-management-commands/
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 ----
I have a Django project. Everytime I deploy, I need to run a series of manage.py command (such as syncdb, south migiration, fixture update).
I am getting tired of type the command line by line and therefore I wrote a python script to do these:
import subprocess
subprocess.call(['python', 'manage.py', 'syncdb'])
#Skip the detail
subprocess.call(['python', 'manage.py', 'loaddata', 'setup/fixture.xml'])
I am wondering if there is a better way to do this?
Thanks.
You can use fabric, a Python library that allows you to script remote actions. This question has some links in the accepted answer for more information on fabric and django.
You can also call management commands directly:
from django.core.management import call_command
call_command('syncdb')
call_command('loaddata', 'setup/fixture.xml')
Save that as a normal python file and execute it from your shell or as part of your deployment scripts.
Right now, if I want to run tests from all my apps, I go:
python manage.py test app1 app2 app3
If I run:
python manage.py test
The test of all apps in INSTALLED_APPS are run, including the django ones. Is there a simple command to run the tests of all the apps that I have created?
Sadly there is no such command. Django has no way of telling which apps are "yours" versus which are someone else's.
What I would suggest is writing a new management command, call it mytest. Then create a new setting MY_INSTALLED_APPS. The mytest command will just run the test for every app in MY_INSTALLED_APPS. You'll want the mytest command to subclass django.core.management.base.AppCommand. django.core.management.call_command will also be helpful.
The only problem with this method is that you will have to constantly maintain the MY_INSTALLED_APPS setting to make sure it is correct.
You could create an management/commands/testmyapps.py for one of your app which has:
from django.core.management.base import BaseCommand, CommandError
import django.core.management.commands.test
from django.core import management
from django.conf import settings
class Command(django.core.management.commands.test.Command):
args = ''
help = 'Test all of MY_INSTALLED_APPS'
def handle(self, *args, **options):
super(Command, self).handle(*(settings.MY_INSTALLED_APPS + args), **options)
This works better in Django 1.6+: when you run python manage.py test, only your tests will run (assuming you have the default settings for TEST_RUNNER)
Use nose django test runner, it takes care of this.
Reference:
Django nose to run only project tests