I want to Create my first URL using the command prompt - python

I have Django installed,but I was trying to create my first URL and I typed the following using the command prompt
"cd myproject" then again I typed "manage.py runserver 127.0.0.1:8000" And I got the following output:
"System check identified no issues<0 silenced>
you have 17 unapplied migration. Your project may not work properly until you apply the migrations for app: admin,auth,contenttypes,sessions."
Please what do I do to fix this problem so that my project can work properly?
Note that: I am new in using Python and Django

You already have an advice to read the tutorial but in case you want to continue on your own (before next trouble), you need to run this in console in order to apply migrations:
python manage.py migrate
Read more about migrations and this command here.

You need to migrate the database first. But it's not error actually. It's just a warning. If you don't want to migrate(create DB tables) just ignore it. And you can still browse 127.0.0.1:8000
Migration command
python manage.py migrate
Read Django documentation very carefully. Django Official Documentation

Related

relation "background_task" does not exist

I'm trying to run some tasks in the background while running a django server. To do this, I'm using background-tasks library. I've followed the following steps:
pip install django-background-tasks
Added 'background_task', in INSTALLED_APPS in settings.py
python manage.py makemigrations background_task
The problem arises in the 3rd step giving an error stating:
django.db.utils.ProgrammingError: relation "background_task" does not
exist LINE 1: INSERT INTO "background_task" ("task_name",
"task_params", "...
I've tried looking for other solutions but every one of them was the 3rd line.
How should I proceed ?
I don't know if this is still valid question but what helped me was to revert all changes in code related to background_task module and then applying migrations. After that reapply your changes in code and it should all work.
After the 2nd step go to command line and do following :
1. python manage.py showmigrations (check if it works fine)
2. python manage.py migrate (check if the background task file is added to the show migrations list eg:
background_task
[X] 0001_initial
[X] 0002_auto_20170927_1109)
3. Now make modifications in your views in which ever way you want to use the backgroundtasks.

Django Heroku and postgresql

I am new to publishing django apps on heroku.
I have read their tutorials and figured I would start simple by modifying their template django project.
So far everything was good.
Then I made an app as one does, made a model and ran
python3 manage.py makemigrations my_app
python3 manage.py migrate
and things seemed ok.
I then pushed to heroku and there were no immediate complaints.
However, now when I try to make save a new model I get the error:
ProgrammingError at /my_app/
relation "my_app_myappmodel" does not exist
LINE 1: INSERT INTO "my_app_myappmodel" ("field1", "field2", "field3") VALUES...
odd...
So I run this locally and everything works fine.
I have tried cleaning my migrations, faking my migrations, squashing my migrations, etc (as other S.O. posts suggest)
Nothing works.
What is up and how do I fix it?
You need to actually run the migrations on Heroku once you have pushed the code generated by makemigrations. You do this via heroku run manage.py migrate.
run the following command from your terminal
heroku run python manage.py migrate
or you can also do:
in your local settings.py, change your DATABASES variable to use the heroku one then run from the terminal
python manage.py makemigrations
python manage.py migrate
but you should not normally locally make changes to the heroku production database (as in option 2) except if you are really desperate or don't care

Applying Django migrations in a non-interactive environment

I've renamed some models in Django and created the migrations using python manage.py makemigrations.
When using python manage.py migrate, I get prompted with the following message
Any objects related to these content types by a foreign key will also
be deleted. Are you sure you want to delete these content types?
If you're unsure, answer 'no'.
Type 'yes' to continue, or 'no' to cancel:
On my local machine, I can simply type 'yes'. However,
my application is deployed on Heroku and I have configured migrations to run automatically when the application is built. I achieve this using a post_compile file that looks like this:
# Run Django migrations
./manage.py migrate
# Compress static assets
./manage.py compress
Will the migration simply fail to complete as a consequence of not being in an interactive shell (and therefore not being able to answer 'yes' to this question)? If so, how can this problem be avoided?
You can use the --noinput command line argument of migrate command:
./manage.py migrate --noinput
This would suppress the prompt, but will not delete stale content types (ie. it works as if you answered No at the prompt). See Django ticket #25036 .
Another alternative would be to use the Unix command yes (I am not sure if it is enabled on Heroku by default though):
yes "yes" | ./manage.py migrate

django syncdb and an updated model

I have recently updated my model, added a BooleanField to it however when I do python manage.py syncdb, it doesn't add the new field to the database for the model. How can I fix this ?
From Django 1.7 onwards
Django has built in support for migrations - take a look at the documentation.
For Django 1.6 and earlier
Django doesn't support migrations out of the box. There is a pluggable app for Django that does exactly that though, and it works great. It's called South.
Django currently does not do this automatically. Your options are:
Drop the table from the database, then recreate it in new form using syncdb.
Print out SQL for the database using python manage.py sql (appname), find the added line for the field and add it manually using alter table SQL command. (This will also allow you to choose values of the field for your current records.)
Use South (per Dominic's answer).
Follow these steps:
Export your data to a fixture using the dumpdata management command
Drop the table
Run syncdb
Reload your data from the fixture using the loaddata management command
As suggested in top answer, I tried using South, and after an hour of frustration with obscure migration errors decided to go with Django Evolution instead.
I think it's easier to get started with than South, and it worked perfectly the first time I typed ./manage.py evolve --hint --execute, so I'm happy with it.
Havent used django in a while, but i seem to remember that syncdb does perform alter commands on db tables. you have to drop the table then run again and it will create again.
edit: sorry does NOT perform alter.
In django 1.6
At first we have run - python manage.py sql <app name>
Then we have to run - python manage.py syncdb
If you run Django with Apache and MySQL, restart apache after making migration with makemigrations.

update django database to reflect changes in existing models

I've already defined a model and created its associated database via manager.py syncdb. Now that I've added some fields to the model, I tried syncdb again, but no output appears. Upon trying to access these new fields from my templates, I get a "No Such Column" exception, leading me to believe that syncdb didn't actually update the database. What's the right command here?
As of Django 1.7+, built-in migrations support, allows for database schema migrations that preserve data. That's probably a better approach than the solution below.
Another option, not requiring additional apps, is to use the built in manage.py functions to export your data, clear the database and restore the exported data.
The methods below will update the database tables for your app, but will completely destroy any data that existed in those tables. If the changes you made to your app model do not break your old schema (for instance, you added a new, optional field) you can simply dump the data before and reload it afterwards, like so:
Django 1.4.15 and earlier
python manage.py dumpdata <your_app> > temp_data.json
python manage.py reset <your_app>
python manage.py loaddata temp_data.json
Django 1.5 and newer
python manage.py dumpdata <your_app> > temp_data.json
python manage.py sqlclear <your_app> | python manage.py dbshell
python manage.py syncdb
python manage.py loaddata temp_data.json
(The reset command was deprecated and then removed in Django 1.5)
If your changes break your old schema this won't work - in which case tools like South or Django Evolution are great.
As of Django 1.7, you can now do this with native migrations. Just run
python manage.py makemigrations <your app name>
python manage.py migrate
Seems like what you need is a migration system. South is really nice, working great, has some automation tools to ease your workflow. And has a great tutorial.
note: syncdb can't update your existing tables. Sometimes it's impossible to decide what to do automagicly - that's why south scripts are this great.
Django's syncdb doesn't alter existing tables in the database so you have to do it manually. The way I always do it is:
Change the model class first.
Then run: manage.py sql myapp.
Look at the sql it prints out and see how it represented the change you are going to make.
Make the change manually using your database manager.
Check to see if everything worked correctly using the admin site.
If you are using sqllite a good manager is the firefox plugin: link
Another tool would be django evolution. No table dropping needed in most cases.
django evolution
Just install it as any other django app and run:
python manage.py evolve --hint --execute
deseb is a great tool for that.
Having it installed, you can write ./manage.py sqlevolve and it'll generate sql commands necessary to keep the database structure in sync with your models.
You need to drop your tables before you can recreate them with syncdb.
If you want to preserve your existing data, then you need to unload your database,
drop your tables, run syncdb to build a new database, then reload your old data into your new tables.
There are tools that help with this. However, in many cases, it's just as easy to do it manually.
For versions 1.4.1 and above users the command has changed to
python manage.py flush
Please read the official document before using it as it will delete all your data.

Categories