Django Testserver fixtures not used - python

I wanted to start a testserver in Django so that I am able to test my API via Jasmine/FrisbyJs.
For that I found that python3 manage.py testserver would create a test-db and load all the testdata provided in the fixtures, which sound exactly what I need. I am NOT running Django-Testcases ATM.
I created a Fixture called testdata.json and stored it within ./fixtures.
I also set up my ./projname/settings.py like that:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Fixture Dir
FIXTURE_DIRS = (
os.path.join(BASE_DIR, 'fixtures'),
)
I also made sure python3 manage.py loaddata testdata works, which it does:
Installed 1 object(s) from 1 fixture(s)
However, running python3 manage.py testserver testdata.json or python3 manage.py testserver testdata leads to this error:
CommandError: Error: No database fixture specified. Please provide the path of at least one fixture in the command line.
What can I do about this?

python manage.py testserver [Fixture Name]
eg :
python manage.py testserver SampleData
SampleData.json file will be in app/fixture/

Related

Manage.py unknown command

I am a student and my profesor needs me to install Django on PyCharm.
I made a big folder called PyCharmProjects and it includes like everything I have done in Python.
The problem is that I made a new folder inside this PyCharmProjects called Elementar, and I need to have the Django folders in there but it's not downloading.
I type in the PyCharm terminal django-admin manage.py startproject taskmanager1 (this is how my profesor needs me to name it)
After I run the code it says:
No Django settings specified.
Unknown command: 'manage.py'
Type 'django-admin help' for usage.
I also tried to install it through the MacOS terminal but I don't even have acces the folder named Elementar (cd: no such file or directory: Elementar) although it is created and it is seen in the PyCharm.
Manage.py its python file after you start your project, you cant call this file until this command:
django-admin startproject mysite
Then run:
python manage.py runserver
And if you want apps in your project run:
python manage.py startapp my_app
First of all, you can't create a project using manage.py because the manage.py file doesn't exist yet. It will be created automatically in the folder taskmanager1 if you run the command below.
You can create a project with the command
django-admin startproject taskmanager1
After that you can change the directory to the taskmanager1 folder with the cd taskmanager/ command.
When you changed the directory you can use the python manage.py commando for example if you want to run your migrations or creating an app.
python manage.py migrate

Django loops infinitly after startapp method call

I'm doing the CS50Web course, and there is a Django project in which I want to create a new app called "wikipedia". The problem is that after I run this command in Windows PowerShell:
python manage.py runserver
this loop happens:
loop print
I don't know what I did wrong for that to happen. After running
python manage.py startapp wikipedia
, I:
went to my Django project and added 'wikipedia' in the installed apps in settings.py
went to urls.py and added a path like this:
path("wiki/", include("wikipedia.urls))
And then I tried running the server. What did I do wrong?
You must have something improperly configured.
Run this:
$ pip uninstall django
$ pip install django
$ django-admin startproject <projectname>
$ cd <projectname>
$ django-admin startapp wikipedia
$ python manage.py makemigrations
$ python manage.py migrate
do what you described in your question
Also, it is always a better idea to run these things in the CMD, rather than in the PowerShell
Are you sure you have url mappings in your wikipedia app urls.py? Not having any patterns there could be the cause of an error.

Python Django Unknown Command 'SQL'

I'm following this tutorial http://www.sitepoint.com/building-simple-rest-api-mobile-applications/
I'm trying to run SQL by
$ python manage.py sql fishes
However it says
Unknown Command: 'sql'
If I type
$ python manage.py help
I get this, and SQL doesn't appear in the list.
Available subcommands:
[auth]
changepassword
createsuperuser
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
sendtestemail
shell
showmigrations
sqlflush
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
test
testserver
[sessions]
clearsessions
[staticfiles]
collectstatic
findstatic
runserver
Unfortunately that doesn't exist any more, however the command manage.py dbshell does.
Generally speaking however, you should try to use models, with load_data etc to preserve the data integrity (as validations etc may happen in models rather then relying on underlying data bits). Or write management commands for any clean up tasks.
Yes it is possible, using the inspectdb command:
python manage.py inspectdb
or
python manage.py inspectdb > models.py
to get them in into the file
This will look at the database configured in your settings.py and outputs model classes to standard output.
As Ignacio pointed out, there is a guide for your situation in the documentation.
Source : link

Django makemessages doesn't see locales in locale_paths

I have custom locale path in my settings
PROJECT_ROOT = os.path.normpath(os.path.join(os.path.dirname(__file__), '..'))
LOCALE_PATHS = (
os.path.join(PROJECT_ROOT,'templates','v1','locale'),
)
but when i'm trying to create .po files, i've get error:
$ python manage.py makemessages --locale=ru
CommandError: This script should be run from the Django Git tree or your project or app tree. If you did indeed run it from the Git checkout or your project or application, maybe you are just missing the conf/locale (in the django tree) or locale (for project and application) directory? It is not created automatically, you have to create it by hand if you want to enable i18n for your project or application.
Why django doesn't want to use LOCALE_PATHS?
Absolute paths are:
PROJECT_ROOT = '/home/creotiv/ENVS/project_env/project/project'
LOCALE_PATHS = ('/home/creotiv/ENVS/project_env/project/project/templates/v1/locale')
PS: Also i added translation to main app and django doesnt see it either.
First things first; You need to give correct path of the manage.py to the python.
$ python /path/to/the/manage.py makemessages --locale=ru
Or go to directory which is included manage.py. After that run that command you've wrote in your question.
$ python manage.py makemessages --locale=ru
Next; Did you create ./templates/v1/locale directories under the project home?
According to error you may check existence of the ./templates/v1/locale' folder.
Also; You may need to append LANGUAGES setting in to project settings.py file:
LANGUAGES = (
('ru', _('Russian')),
('en', _('English')), # (Optional)
)
Appendix:
If you are developing under virtual environment, do not forget to enable virtual environment first.
Maybe you have to go inside the app or project directory. Try this:
$ ls # should return also the manage.py
$ cd myproject
$ ls # should return also the settings.py
$ python ../manage.py makemessages --locale=ru
Just updated to django 1.7 and problem gone.

fabric does not work with my python/django code

Here is my content of fabfile.py
from fabric.api import run, local, abort, env, put, task
from fabric.contrib.files import exists
from fabric.context_managers import cd, lcd, settings, hide
from fabric.operations import require
PROD_SERVER='user#user.webfactional.com'
# Host and login username:
def prod():
env.hosts=[PROD_SERVER]
env.remote_app_dir='~/webapps/django/myapp/'
env.remote_apache_dir='~/webapps/django/apache2/'
def commit():
message=raw_input("Enter a git commit message: ")
local("git add -A && git commit -m '%s'" %message)
local("git push webfactioncarmarket")
print "changes pushed to remote repository...."
def test():
local("python2.7 manage.py test")
def install_dependencies():
with cd(env.remote_app_dir):
run("pip2.7 install -r requirements.txt")
def testing_fabric():
prod()
print env.hosts
print env.remote_app_dir
print env.remote_apache_dir
def collectstatic():
prod()
print env.hosts
require('hosts',provided_by=[prod])
run ("cd $(remote_app_dir);python2.7 manage.py collectstatic --noinput
Running :
fab testing_fabric #works fine
['user#user.webfactional.com']
~/webapps/django/myapp/
~/webapps/django/myapp/
Done.
Running:
fab collectstatic
['user#user.webfactional.com']
No hosts found. Please specify (single) host string for connection:
why do I get this prompt: No hosts found. I print env.hosts and it prints the host?
how do I fix this?
EDIT: I tried the chaining:
I get this error:
run: cd $(remote_app_dir);python2.7 manage.py collectstatic --noinput
out: /bin/bash: remote_app_dir: command not found
why can I not pass the value of remote_app_dir does not get passed?
There are two ways to do this:
Set env.hosts at the module level, so it gets set when the fabfile is imported
Chain modules to set the env variable.
In your case, the second one can be accomplished by removing the line prod() from collectstatic and instead invoking fabric like this:
fab prod collecstatic
This sort of decoupling can be immensely powerful. See here: http://fabric.readthedocs.org/en/latest/usage/execution.html#globally-via-env
To run from a specific directory, try using with cd:
def collectstatic():
with cd(env.remote_app_dir):
run("python2.7 manage.py collectstatic --noinput")
See here: https://fabric.readthedocs.org/en/latest/api/core/context_managers.html
I write answer, becaue I don't have enough points to make a comment:
run ("cd $(remote_app_dir);python2.7 manage.py collectstatic --noinput
You should not use remote_app_dir variable (wrapping in it $() ) as you would do using bash/shell. First of all, this variable is not available in shell.
env.remote_app_dir='~/webapps/django/myapp/'
cause that python object env gets new attribute remote_app_dir
please change your code and use your variable in pythonic way:
run ("cd %spython2.7 manage.py collectstatic --noinput" % env.remote_app_dir)

Categories

Resources