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
Related
So I'm following the official tutorial (creating a polls app) for django and I'm having db issues. For some reason I didn't have sqlite3 included with python (not a big issue, I just installed it). After installation, I have a connection in settings.py like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
I ran python migrate.py migrate. The next step was to run sqlite3 and do this:
.schema
Nothing shows up, because no DB exists. I'm very confused as to what I may not be doing right (or what I have done to mess it up) to get a proper database connection for the app.
sqlite is a file-based db, so you need to tell it what database file to open. Either do it explicitly:
sqlite3 path/to/db.sqlite3
or, better, use Django's shortcut:
./manage.py dbshell
I have a Django project that uses MySQL as its database. On my development machine the MySQL database runs on my local machine as shown here:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'xxx',
'USER': 'root',
'PASSWORD': 'xxxx',
'HOST': 'localhost',
#'PORT': '3306',
}
}
I have successfully deployed the project to Heroku, but it keeps showing that
2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection refused)
since the setting is the local MySQL and the views.py contain functions that import local database. I still cannot figure out how to have the website to connect to this database.
Is there a way to make this work with Heroku, or do I need to find another host?
juanmhidalgo's answer is a good start, and can be generalized for arbitrary environment variables. But if you only care about the database variable there's another solution.
By default, Heroku provides a PostgreSQL database and sets your application's DATABASE_URL with a connection string that can be used to connect to it.
As far as I know, the various MySQL addons also set this variable but it would be helpful to know which one you've selected so we can confirm. It may need to be set manually, based on another environment variable.
Assuming that the DATABASE_URL environment variable is properly set you can use dj-database-url to set your database up directly, optionally providing a fallback to use when the variable isn't available (e.g. on your development box), in your settings.py file:
import dj_database_url
DATABASES['default'] = dj_database_url.config(
default='mysql://root:<password>#localhost:3306/<database>',
)
You can promote the settings.py to a package. Create the settings folder like this one
settings/
├── __init__.py
├── base.py
├── dev.py
├── heroku.py
Keep the base.py as your settings.py and then, based on the env, changes what you need.
Your heroku.py could be
from .base import *
DATABASE = {
# change to use the database on heroku
}
What is important is to tell the deploy version to use the new settings. Check the wsgi.py
import os
from django.core.wsgi import get_wsgi_application
# point to the new settings.dev as default, if the `DJANGO_SETTINGS_MODULE`
# is not set
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings.dev")
application = get_wsgi_application()
Remember to change the DJANGO_SETTINGS_MODULE in heroku to settings.heroku
A really useful way to do this is to use django-environ and set the configuration option on the heroku settings.
# settings.py
# Parse database connection url strings like psql://user:pass#127.0.0.1:8458/db
DATABASES = {
# read os.environ['DATABASE_URL'] and raises ImproperlyConfigured exception if not found
'default': env.db(),
}
and your environment variable like:
DATABASE_URL=mysql://user:%23password#127.0.0.1:3306/dbname
Regarding your question, Heroku is a really good option to deploy your projects.
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
...
}
}
I'm quite a newbie in Django, and web dev in general. I've been following tutorials and guides in making a Django project locally. Now, I want to deploy my project to WebFaction. I followed all of their instructions found here: https://docs.webfaction.com/software/django/getting-started.html
However, after doing all of these, when I go to the domain, it simply says:
Internal Server Error
The server encountered an internal error or misconfiguration and was
unable to complete your request.
Please contact the server administrator at [no address given] to
inform them of the time this error occurred, and the actions you
performed just before this error.
More information about this error may be available in the server error
log.
The app I made is based on Django 1.8.6, mod_wsgi 4.4.21, and Python 3.4 as per WebFaction's one-click setup. I'm sticking to the default Apache + mod_wsgi, and PostgreSQL for my database. Packages I pip installed include the likes of:
django-allauth==0.23.0
django-analytical==1.0.0
django-crispy-forms==1.5.2
django-postman==3.3.1
django-haystack==2.4.0
elasticsearch==2.0.0
Pillow==3.0.0
My production settings.py include the ff:
DEBUG = False
ALLOWED_HOSTS = [
'mydomain.ph',
'www.mydomain.ph',
'myusername.webfactional.com'
]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': '< my_db_name >',
'USER': '< my_db_user >',
'PASSWORD': '< my_db_pass >',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
STATIC_ROOT = '/home/<my_wf_user>/webapps/static/'
ADMINS = (('<my_name>', '<my_email>'), ('',''))
One discrepancy I noticed was that python3 on the server had a default version of 3.5 (checked with python3 -V), hence I created a dir: ~/lib/python3.5, but in my app, Python 3.4 was installed in the one-click setup, so I have a dir: ~/webapps/<proj_name>/lib/python3.4
I have been trying to crawl through the web, but either can't find anything useful or end up with incredibly difficult to understand jargon that may not even be what I need. Help would be greatly appreciated! Thank you
Look into your Apache log file, it should contain the error encountered either at the Apache level or mod_wsgi level. I think the log will be located at the user log folder. If it's not there you can also check the front end log.
Thank you for all of your tips and instructions! They did lead me to my eventual solution, as the errors showed that the django packages "were not installed" (but they were!) so in accordance with my initial speculation, I simply changed ~/lib/python3.5 to ~/lib/python3.4 and it became alright.
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.