Its been a few months that I am trying to learn Django. In the same process (and while reading "Two Scoops of Django 1.11"), I came across Cookiecutter Django. It has helped me learn a few important things to keep in mind while creating a project.
I tried to run the template provided by cookiecutter-django but failed. Here are the steps that I followed.
Create a virtual environment named test and activate it.
mkvirtualenv test
Installed Cookiecutter.
pip install coockiecutter
Installed Cookiecutter Django, The project name was set to "Test Project" and other defaults settings were chosen. I am using PostgreSQL 9.6.
cookiecutter https://github.com/pydanny/cookiecutter-django
Create a database named "test_project" in PostgreSQL.
Run python manage.py migrate
The result was the error:
django.db.utils.OperationalError: FATAL: role "dev" does not exist
I have also tried making a user named test_project_user and granting it all the privileges to test_project database. I am still getting the same error.
The problem seems to be that you specified a database user that does not exist (or you left blank and it assumes your system user), in:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'test_project',
'USER': 'HERE', # Set test_project_user here
...
}
}
Related
I installed djongo through pip. My django version is 3 although I checked version 2 also to make it work. Python version is 3. I also made change in settings.py but whenever I load the command with makemigrations, it gives me following error.
Try using 'django.db.backends.XXX', where XXX is one of:
'mysql','oracle','postgresql','sqlite3'
I don't know what to do with this now?
settings.py
DATABASES = {
default: {
'ENGINE': 'djongo',
'NAME' : 'django_db'
}
}
there's this awesome package that allows you to connect Django with MongoDB it's called mongoengine
http://mongoengine.org/
here's also a link for their documentation
http://docs.mongoengine.org/
In my project, I have a routers.py with different router classes. Now, I am making a new app. I have created my models.py. I have also registered the app in the INSTALLED_APPS in settings.py. Then, I ran validate. Everything is fine. When I syncd thoughb, Django does not install the tables. I tried using
python manage.py sqlall <app_name> | psql <database>
Then, I get an error message saying:
psql: FATAL: password authentication failed for user <user name>
I noticed that the role does not exist in postgres. So, I created the role with login privilege createdb and password. Then, I get a different error message:
psql: FATAL: password authentication failed for user <user name>
close failed in file object destructor:
Error in sys.excepthook:
Original exception was:
And, it does not provide the original exception.
Any help is much appreciated.
It looks like the Django application is unable to log onto the DB.
In django's settings.py make sure you have the proper DB credentials setup:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'HOST': get_env_variable("DJANGO_DB_HOST"),
'NAME': get_env_variable("DJANGO_DB_NAME"),
'USER': get_env_variable("DJANGO_DB_USER"),
'PASSWORD': get_env_variable("DJANGO_DB_PWD"),
'PORT': '',
},
...
}
As you can see my credentials are grabbed from environment variables. You can hardcode them in for test purposes.
Then in the DB (mine is postgresql) create the user/grant it the correct privileges, for example:
ssh root#dbhost
su - postgres
createdb dbname
psql
GRANT ALL PRIVILEGES ON DATABASE dbname TO dbuser
That should do.
I recommend the following steps as well, if they are missing from your setup:
In your app's admin.py register your models with Django's admin:
# Register your models here.
admin.site.register(Model1)
...
admin.site.register(ModelN)
Then, assuming you have created the project already, run:
python manage.py migrate
(it's the syncdb equivalent. Read the docs about migrations).
If that command does not ask for the admin superuser then create your administrative user (i.e. the user who can manipulate the models through django's admin interface) with:
python manage.py createsuperuser
Fire up Django
python manage.py runserver 0.0.0.0:8000
and see what happens whan you visit your site and admin at
localhost:8000/
localhost:8000/admin
Please pardon me if you know all those things already. That is what I normally do in my dev environment (I also use virtualenv, of course).
I'm trying to deploy my Django application to Heroku. The migrations are in my local Git. When I try:
git push heroku master
heroku run python manage.py syncdb
It applies the migrations and also promts me to create superuser, which I successfully do. Now the application is up and running, however when I try to log into the Django admin it's throwing:
OperationalError no such table: user_user
When I try
heroku run python manage.py makemigrations
heroku run python manage.py migrate
heroku run python manage.py createsuperuser
It applies all migrations, but fails to create superuser throwing:
django.db.utils.OperationalError: no such table: user_user
Either way I can not have my database set up and migrated on Heroku.
My database settings are:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
My user model is:
class User(AbstractUser):
rating = models.PositiveIntegerField(default=settings.DEFAULT_USER_RATING)
Django version is 1.7.1.
How do I get my database tables created on Heroku?
You must not use sqlite3 on Heroku.
sqlite stores the database as a file on disk. But the filesystem in a Heroku dyno is not persistent, and is not shared between dynos. So, when you do heroku run python manage.py migrate, Heroku spins up a new dyno with a blank database, runs the migrations, then deletes the dyno and the database. The dyno that's running your site is unaffected, and never gets migrated.
You must use one of the Heroku database add-ons. There is a free tier for Postgres. You should use the dj-database-url library to set your database settings dynamically from the environment variables which Heroku sets.
Also, for the same reason, you must do manage.py makemigrations locally, commit the result to git, then push to Heroku.
You can use postgresql:
In settings.py add(at the end of file):
# ie if Heroku server
if 'DATABASE_URL' in os.environ:
import dj_database_url
DATABASES = {'default': dj_database_url.config()}
In requirements.txt add:
dj-database-url
psycopg2
Now you can run:
heroku run python manage.py migrate
pip install django-heroku
Add import django_heroku at top of file settings.py
Place django_heroku.settings(locals()) at the very bottom of settings.py
It will automatically configure your db. You can learn more on their website
What version of django you are using..?
If you are using django>=1.7 you need to run migrate
After adding models you need to do
python manage.py makemigrations then python manage.py migrate
If your project already contain migrations you can directly run python manage.py migrate command.
If you miss any step mentioned above please do that.
I'm trying to start a Django app to be hosted on GAE, using CloudSQL. Locally, I'm on Mac OSX Maverics, working within a virtualenv (via virtualenvwrapper).
After installing the GAE SDK for Python, I started my virtual environment, installed Django 1.5 from /usr/local/google_appengine/lib/django-1.5/
Also, on appengine.google.com I created a new app, and connected a CloudSQL instance to it (enable billing).
I'm able to create a new Django project, e.g. django-admin.py startproject test01, then I edit its settings.py to change the DATABASES definition per Google's instructions, e.g:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '/cloudsql/myapp-test01:myapp-db-test01',
'NAME': 'test01',
'USER': 'test01',
}
}
I also added app.yaml to the root of the project folder, per Google's docs:
application: test01
version: 1
runtime: python27
api_version: 1
threadsafe: true
libraries:
- name: django
version: "1.5"
builtins:
- django_wsgi: on
This is where I hit roadblocks.
First: What exactly should be entered into the DATABASES for NAME and USER fields? The docs do not go into any detail.
Second, when I run: python manage.py syncdb to initialize the app, I get:
OperationalError: (2002, "Can't connect to local MySQL server through socket '/cloudsql/desgn-test-01:db-test-01' (2)")
I do have MySQL installed, via brew install mysql (although I didn't do that inside the virtual environment), and I also have MySQL-python.
I'm new to GAE and fairly inexperienced with setting up databases, so I'm not sure what do try next. I'm not sure if the issue is with my local MySQL, or the CloudSQL connection settings?
(A more general Django on GAE question: What is the workflow exactly? If I get the connection to work, does it mean that I am using CloudSQL even when developing my Django app locally? How do I subsequently "push" the app to the AppEngine, or make updates? I'm assuming this is done with the Launcher but what is the correlation between creating (adding) an app using the appengine.google.com dashboard, versus adding a new app in the local launcher? Quite confused by this -- are these two one and the same app, need to have identical names, or..?)
Looks like at the time, you were missing the password field as well...
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test01',
'USER': 'test01',
'PASSWORD': '[password]',
'HOST': '/cloudsql/myapp-test01:myapp-db-test01',
'PORT': '3306',
}
Using the GUI for Cloud SQL, you can always add new Super Admin level users to the Cloud SQL instance in case you're unsure what user to use as well.
I've been trying to set up my Django project with MAMP for hours,, but still having problem understanding what's going on..
So what I've been doing is:
-First, obviously I installed all the necessary packages (ex. mysql, mysql-python, etc)
-I changed the MAMP's Apache Document Root to Django project folder (/MyDjangoProjects/Sample_Project/)
-I changed the Sample_Project's setting.py to:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'samleprojectdb',
'USER': 'root',
'PASSWORD': 'password1',
'HOST': '/Applications/MAMP/tmp/mysql/mysql.sock',
'PORT': '',
}
}
-Finally, I ran the Apache and MySQL servers with MAMP and navigated localhost:8888
So I guess basically theses are all the necessary steps that I need to take...
I expected that navigating the page localhost:8888 will direct to my project's main page view (index.html), as I configured in urls.py. However, it just opens an "Index of /" page containing the directories and python files in my local directory. I know MAMP is intended to be used with PHP and to look for index.php, but I thought it should work with Django projects as well..
1. Is there something else I need to do to test Django projects with MAMP??? Thanks...
2. Also, where is the database file "sampleprojectdb" created???? When I used sqlite instead of mysql, the database file was automatically created in the project directory when I ran "python manage.py syncdb"
The host expects the address of the machine that runs the mysql server,like localhost or an IP of the system that runs the DB server.
Also,Use blank for localhost..
About where the database is created,if you mean the files,they are at /var/lib/mysql,but they wont be of much use.syncdb creates the database with the specified configuration.
Also, to run django with apache you will have to setup apache to run the wsgi daemon.
You can try your application using the development server by python manage.py runserver