Django Tutorial - python

I'm just starting with the Django tutorial. When I try to run python manage.py migrateas they say, I get
Unknown command: 'migrate'
Type 'manage.py help' for usage.
I am in the directory right above manage.py. What am I doing wrong here? Thanks!

You may be using the wrong Django version. You can figure out which version you're running by doing python ./manage.py shell and then inside the shell, type the following:
>>> import django
>>> django.VERSION
If your django version is less than 1.6, check the docs for the version applying to what you are running.
Previous versions of Django did not use the migrate command, unless you installed a third party module called South.

Related

AttributeError: module 'collections' has no attribute 'Iterator'

I'm diving into a Django docs and I'm using the latest Django (4.0.4) and Python 3.10.0
Whenever I try the command django-admin startproject mysite it gives the error "AttributeError: module 'collections' has no attribute 'Iterator'"
I am using a virtual environment as well which has the Django installed on it.
I tried uninstalling the django and reinstalling but no changed in the program..
Please guide me into the right direction.
Thank you!
Django 4.0.4 was just released few days back.
You know, django-admin is a Python script that gets added to your PATH variable so that it's invocable from the command line with django-admin (as opposed to something like python3 -m django_admin) maybe there was an older version lurking around your PATH that reinstalling will help you fix
or
Use an older version of Python (Django 2.x, as used by that project supports up to Python 3.7).

How can I show the django version in pycharm?

I want to see the django version in my Pycharm terminal, but I don't get the correct method.
I tried bellow methods in pycharm terminal:
1) django --version and django version
2) import django, and print the version by:
import django
print django.VERSION
But I still can not get it.
If you cannot print the Django version from the python console in Pycharm, go to settings>Project:project_name>project Interpreter and from the list of installed packages see the installed Django and it's version for that project.
You can run pip freeze too. Just filtering the results with grep...
pip freeze | grep Django
You're trying to access a version attribute, but you can find out Django's version using the get_version method:
import django
django.get_version()
In the terminal you can use bellow command to check the version of django:
python -m django --version
If you want to use your second method, just like this bellow command:
python -c "import django; print(django.VERSION)"
(1, 11, 2, u'final', 0)
go to setting > project > project Interpreter and install Django package
then run the code
import django
print(django.get_version())
Run pip freeze > requirements.txt
A .txt file with that name will be created automatically, open the file and check for the django version and other previously installed libraries.
I do not know about pychram but the most efficient way to query the django version without importing django globally would be this:
from django import VERSION as DJANGO_VERSION
if DJANGO_VERSION >= (2, 0):
pass
What was not proposed in this question which is something similar:
How to check Django version
I had the same question. The solution is simple.
Just create a project, open settings.py file and observe the first comment lines. You will see the version info in there as displayed below:
Image of settings.py that shows the Django version
I found the solution here.
Simplest is
import django
print(django.get_version())
Using the following command on terminal will give you django version
python -m django --version
Or
You can go to the interactive python prompt and use the command
import django
print(django.get_version())
or using pip freeze and grep you can also get the django version
pip freeze | grep Django

Django python manage.py migrate

I have installed on Win7 portable Python 2.7.5.1 and Django 1.6.
I followed the first polls tutorial instructions and got an error in the migrate stage, python manage.py migrate:
C:\Natan\Dev\Portable Python 2.7.5.1\App\Scripts\mysite>..\..\python.exe manage.py migrate
Unknown command: 'migrate'
Type 'manage.py help' for usage.
Any idea?
If you've installed 1.6, you should use the 1.6 tutorial, not the one for the development version.
First Step, Install South:
pip install south
Second Step, Add South to INSTALLED APPS in settings
INSTALLED_APPS = (
...,
'south' )
Migrate will be a native command in 1.7 (which is the version you read the tutorial pages from).
For older versions, you'll have to install the third party app"South".
All-
This is likely caused largely by following the 1.7 (DEV version!) tutorial when we all get the last stable version (1.6!) installed by pip.
It would not appear migrate is even a part of 1.7 in general! Will:
python manage.py syncdb
Solve your problems?
So pls either follow the tutorial for the last stable version of django: https://docs.djangoproject.com/en/1.6/intro/tutorial01/
Or follow the instructions to install the dev version of Django.
Dear Django team,
You guys are awesome. The JS developer is very impressed. But PLEASE resolve the discrepancy above. Maybe default to 1.6 docs and put in red letters: Dev version here?
Thank you.
You must install South to add migrate command.
pip install south
If you are using Django older versions before 1.7 then you should sync database by
python manage.py syncdb
while in new versions after 1.7 syncdb is replaced by migration. So for syncdb in new django version is replaced by :-
python manage.py makemigrations
python manage.py migrate
for more type:-
python manage.py help

Running django-admin.py startproject is giving me an error

I've just started looking into python and django.. Im pretty sure ive successfull installed both python and the django framework and i managed to add python and django-admin.py to my system path but now when i run the command django-admin.py startproject My_Test_Site, in the folder i want to create a new project directory in (just following the tutorials), i get the following error:
What am i doing wrong?
This is not a Django error. It appears it cannot find the Python module unicodedata which is part of the Python Standard Library (docs). I see that there is also a bug report about this issue (Python 2.7 on Windows, see here). On the other hand, some people there report that reinstalling their Python installation fixed the problem. You could give that a try.
notice the last line, it says import unicodedata, importError
im guessing you probably didnt install your django properly.
so reinstall it and check that django is in your "path", i believe django does this automatically when it installs but doesnt hurt to double check.
if reinstalling django still doesn't work, i suggest you uninstall python and django related stuff.
then install python, install pip, then use pip install django.

Installing django-haystack

Super simple question: I went through the "getting start" doc for haystack (using whoosh; I installed both python-whoosh and haystack using pip (first time using it)), and I simply cannot run python manage.py rebuild_index. I get this error:
python manage.py rebuild_index
Unknown command: 'rebuild_index'
Type 'manage.py help' for usage
I do have 'haystack' listed in my INSTALLED_APPS in settings.py, so this solution doesn't seem to work for me. I also don't get import errors when I run "import haystack" so it does exist. I tried this on my existing project as well as a brand new object made just for this and I just can't get it to work.
However, when I import haystack after python manage.py shell and try haystack.__version__ I get "AttributeError: 'module' object has no attribute 'version'". I get a similar error if I try haystack.management: it has no management attribute.
It must be something super simple I'm missing. Thank you for reading this!
Did you perhaps install the wrong thing? This (embarrassingly) happend to me just today. Make sure you install 'django-haystack' and not just 'haystack' (and you will have to remove 'haystack', since it conflicts with 'django-haystack').
Do you have the path to haystack in your Python path? (Either the PYTHONPATH shell variable or the sys.path Python list.)
Did you run python manage.py syncdb?
Does python manage.py shell followed by import haystack work?
After import haystack, what do you get for haystack.__version__?
In the same shell, type the following. Do you get errors for any of them?
haystack.management.commands
haystack.management.commands.rebuild_index
haystack.management.commands.rebuild_index.Command.help
I had the same error that you did, and fixed it by removing the old .egg and installing directly from the latest version. You can use easy_install:
easy_install https://github.com/toastdriven/django-haystack/zipball/v1.2.4
Hope this helps!
I had the same problem - for whatever reason, the haystack version in pip is quite old and obsolete (v 0.6 as of today). To use django-haystack check out the v1. source.
It compiles and installs simply with
python setup.py build
python setup.py install
Hope that helps!
I just had the same problem and hadn't set HAYSTACK_SITECONF in my settings.py.
Instead of using python manage.py rebuild_index try using ./manage.py rebuild_index
just do
pip uninstall haystack and
pip install django-haystack

Categories