I'm following the django tutorial, and I don't understand why put the templates folder for the admin pages in the base directory, instead of having it in mysite directory.
In fact, when adding templates to the polls app, these were put in the polls directory. Is there an underlying reason for this to happen? Will I run into trouble if I change this configuration?
An issue I ran into once with putting templates in apps’ template directories was that the template loader would use the first template it found by looping through the INSTALLED_APPS setting, so if your app comes after the admin app in the list (which it probably does), templates overriden there won’t be used.
Related
Him I am trying to set up my first django project. I have never done Django before so I don't know anything about it. I am trying to follow the official Django tutorial page, it tells me to open views.py. I can't find it anywhere in my "mysite" folder. In it, I have manage.py , db.sqlite3, __init__.py, settings.py, urls.py and wsgi.py along with the pycache folder, but no signs of views.py. Can anyone help me?
Running this command in console django-admin startproject myfirstproject will create a new django project for you, which will have another sub-folder named same as your project name. By default on creating a new project, the views.py file isn't created you have to create it manually in your project sub-folder.
To create a new app, Run django-admin startapp myapp which will create a new sub-folder under your parent directory which will have views.py where you can write your own custom views for your HTML templates
I got a basic Django setup working on my webhost, then I copied it down to my local machine. Both my local and my remote are using the same version of Django, and the same database (Postgres) with the same exact settings.py.
The thing is, when I run manage.py runserver on my local, and then browse to localhost, I get:
ViewDoesNotExist at /
Could not import myapp.main. View is not callable.
The view does exist though, and works perfectly fine on my remote. When I look at the PYTHONPATH in the debug output it includes the base directory of my django setup, and the "main" app is in the INSTALLED_APPS in my settings.py.
Basically everything is setup the same as on my remote (except that the remote is using Apache and I'm using runserver), at least as far as I can tell, but they are behaving differently. Does anyone have any clue what could be wrong?
EDIT:
It turns out I'm an idiot, and one of my files (urls.py) on the server never made it in to the commit. As a result, my local file wasn't what I thought it was, and I failed to realize the problem. Once I updated urls.py everything worked.
put 'myapp' in your settings.py
INSTALLED_APPS = (
'myapp',
)
urls.py
url(r'^$', 'myapp.views.main', name='main'),
check that in views.py, a function named 'main' should be there
First of all I have found several solutions for my problem but no one fits. So i use Django 1.5 + Python 3.3 + Gunicorn + Nginx as webserver.
My Django project directory look like:
fv/
frontend/
static/
templates/
index.html
models.py
views.py
fv/ -- this is only a folder where logic calculating files are stored
media/
static/
manage.py
settings.py
urls.py
wsgi.py
Now I would like to have the app homepage like frontend but these two apps should be like a standalone website which different domains like www.homepage.com and www.frontend.com. But they should use the same model, because the only model table I would like to share is the Django user model. And every app should have his own template files and urls.py.
I've already read the Django sites framework documentation but i have no idea how to redesign my project dir and arrange other necessary parts like the settings.py, urls.py.
I hope anyone could help me? :)
I have a problem setting my Django application for deployment on openshift and testing locally.
Here is my structure
root_folder/
my_project/
anoter_app/
urls.py
views.py
my_project/
settings.py
urls.py
views.py
manage.py
application.py (to tell openshift where my settings file is: my_project.myproject.settings)
So for it to work on the deployment server, in the settings, the ROOT_URL_CONF is:
myproject.myproject.urls
and in my url file, the view must be reached as myproject.myproject.views
But when I want to work locally, I have to change the ROOL_URL_CONF as myproject.urls
and the views are reached with myproject.views
How do I make it work both locally and on the deployment server with the same settings?
Thank you
Create a new file named local_settings.py, at the bottom of your settings.py add:
try:
import local_settings
except:
print 'CAUTION -- NOT USING LOCAL SETTINGS!'
Put any settings you need to override on your local environment in your local_settings.py file.
I resolved it, the problem was that the folder and the app had the same name.
I renamed the app and now i dont'have to do myproject.myproject
I'm new to Django. I'm using Django with Eclipse. I've created a Django project using Eclipse (called "Django_Test_Project"). I've also created a PyDev project outside of Eclipse, using the command line (called "polls"). It has models.py, views.py, and tests.py.
I created "polls" using the following command:
manage.py startapp polls
I want use Eclipse to add "polls" as a second project to "Django_Test_Project". How do I do that with a project that was created outside of Eclipse? Eclipse doesn't recognize "polls" as a project, probably because the project files are missing in "polls".
Any help is appreciated. Thanks.
You are working on the Django tutorial, right? First of all, your terminology is not correct. You confuse a project with an application or app for short. It's no surprise that Eclipse doesn't recognize polls as a project, because it's not a project but an app.
In Django 1.4.1, the standard structure for a project called mysite is this:
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
Your polls app should go in the same directory where the file manage.py is located:
mysite/
manage.py
polls/
__init__.py
models.py
tests.py
views.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
You can just move the polls directory into the mysite directory using the Windows Explorer, Finder, Terminal etc. (depends on which OS you are running on). After refreshing the project view in Eclipse, your polls app should show up. In any case, you should read the Django tutorial more carefully as it basically answers your question already.
Additionally, take a look at this thread that explains the difference between projects and apps in a bit more detail.