setting up homepage, and naming/organizing apps

22 views
Skip to first unread message

Alex Hall

unread,
Jul 18, 2011, 11:49:00 PM7/18/11
to django-users
Hello all,
I was on this list a few months ago, but was unable to get a
django-friendly web host. I am now helping with a site on Bluehost,
which does support django, and I am hoping to be given permission by
the guy in charge to use django instead of php for our project.

I have been going through the tutorial, adapting it to my project as
necessary, and have a couple questions.

1. This must be glaringly obvious, but how do I use django to load my
homepage (index.html in the public_html root)? I understand about
views and url matching, but there is no views.py in the main directory
and I am not sure if I am supposed to create one or not. In other
words, how does django know where to find the page to load when a
visitor simply goes to mysite.com, instead of
mysite.com/something_to_match?

2. The project I am working on has a few tables: media, articles, and
authors. An article can have one author and one or more rows in the
media table associated with it. I currently have an app called
"tables", in which I plan to define the three tables in models.py (I
am only working with authors as a test). All my views, urls, and so
forth will be inside this tables app. Is this generally recommended,
or should I have an app for each table? The tutorial has two tables
defined, but the urls are all /polls/[something], whereas my urls
might be /authors/, /author/[id], /article/[id], and so on (not
sharing /[app_name]/... like the tutorial does). I hope that made
sense!

--
Have a great day,
Alex (msg sent from GMail website)
meh...@gmail.com; http://www.facebook.com/mehgcap

Daniel Roseman

unread,
Jul 19, 2011, 3:04:15 AM7/19/11
to django...@googlegroups.com
On Tuesday, 19 July 2011 04:49:00 UTC+1, Alex wrote:
Hello all,
I was on this list a few months ago, but was unable to get a
django-friendly web host. I am now helping with a site on Bluehost,
which does support django, and I am hoping to be given permission by
the guy in charge to use django instead of php for our project.

I have been going through the tutorial, adapting it to my project as
necessary, and have a couple questions.

1. This must be glaringly obvious, but how do I use django to load my
homepage (index.html in the public_html root)? I understand about
views and url matching, but there is no views.py in the main directory
and I am not sure if I am supposed to create one or not. In other
words, how does django know where to find the page to load when a
visitor simply goes to mysite.com, instead of
mysite.com/something_to_match?


I don't know why you mention "index.html in the public_html root". Django pages are dynamic and are not served from the html_root. You could set up your webserver to look there for URLs matching / only, but are you sure you don't have *any* dynamic or database-based content in your home page? Not even a menu? Seems strange. Normally, you would just set up a url to match r'^$' and route to a view in the normal way.

2. The project I am working on has a few tables: media, articles, and
authors. An article can have one author and one or more rows in the
media table associated with it. I currently have an app called
"tables", in which I plan to define the three tables in models.py (I
am only working with authors as a test). All my views, urls, and so
forth will be inside this tables app. Is this generally recommended,
or should I have an app for each table? The tutorial has two tables
defined, but the urls are all /polls/[something], whereas my urls
might be /authors/, /author/[id], /article/[id], and so on (not
sharing /[app_name]/... like the tutorial does). I hope that made
sense!


No, you have it right. These three models are clearly all related, so belong together in one app.
--
DR. 

Venkatraman S

unread,
Jul 19, 2011, 3:06:07 AM7/19/11
to django...@googlegroups.com
On Tue, Jul 19, 2011 at 9:19 AM, Alex Hall <meh...@gmail.com> wrote:

1. This must be glaringly obvious, but how do I use django to load my
homepage (index.html in the public_html root)? I understand about
views and url matching, but there is no views.py in the main directory
and I am not sure if I am supposed to create one or not. In other
words, how does django know where to find the page to load when a
visitor simply goes to mysite.com, instead of
mysite.com/something_to_match?


Dont you have a urls.py in your project? Something like this will take you to the "/" of the site:
url(r'^$'                ,'index'         ,{}, name="index"),

 
2. The project I am working on has a few tables: media, articles, and
authors. An article can have one author and one or more rows in the
media table associated with it. I currently have an app called
"tables", in which I plan to define the three tables in models.py (I
am only working with authors as a test). All my views, urls, and so
forth will be inside this tables app. Is this generally recommended,
or should I have an app for each table? The tutorial has two tables
defined, but the urls are all /polls/[something], whereas my urls
might be /authors/, /author/[id], /article/[id], and so on (not
sharing /[app_name]/... like the tutorial does). I hope that made
sense!

Since all the 3 tables relate to a 'single' functionality - this is fine.

-V

Mike Dewhirst

unread,
Jul 19, 2011, 3:36:07 AM7/19/11
to django...@googlegroups.com
On 19/07/2011 1:49pm, Alex Hall wrote:
> Hello all,
> I was on this list a few months ago, but was unable to get a
> django-friendly web host. I am now helping with a site on Bluehost,
> which does support django, and I am hoping to be given permission by
> the guy in charge to use django instead of php for our project.
>
> I have been going through the tutorial, adapting it to my project as
> necessary, and have a couple questions.
>
> 1. This must be glaringly obvious, but how do I use django to load my
> homepage (index.html in the public_html root)? I understand about
> views and url matching, but there is no views.py in the main directory
> and I am not sure if I am supposed to create one or not. In other
> words, how does django know where to find the page to load when a
> visitor simply goes to mysite.com, instead of
> mysite.com/something_to_match?

Normally you would build a dynamic home page but here is a simple
sequence ...

1. Write a hello world index.html which will be your "home" page

2. Put it in <project>/templates where <project> is the directory
containing settings.py

3. In urls.py where your other patterns exist scroll to the end and add
this ...

# note the += which appends this pattern to the others.
urlpatterns += patterns('',
# empty string r'' matches any URL so put it last
(r'', 'flatviews.flatpage'),
)

4. Create a new file flatviews.py in the <project> directory like this ...

from django.shortcuts import render_to_response

def flatpage(request, pagename='index.html'):
return render_to_response(pagename)

Should do the trick. Your mission is now to build your home page from
inherited templates and database content via the django template language.


>
> 2. The project I am working on has a few tables: media, articles, and
> authors. An article can have one author and one or more rows in the
> media table associated with it. I currently have an app called
> "tables", in which I plan to define the three tables in models.py (I
> am only working with authors as a test). All my views, urls, and so
> forth will be inside this tables app. Is this generally recommended,
> or should I have an app for each table?

The layout principle as set down by James Bennet in Practical Django
Projects is to keep it simple. In other words you would begin by keeping
it all in a single app directory.

If it starts to get complex you can refactor into separate directories
eg all views in a views directory etc. In there you can have different
files for different groups of views which might have some similarity
such as all needing to import the same particular model or models.
Models too can be split into different files in a separate models
directory. Keep stuff together which belongs together.

Finally, he says to try and segregate your own project into separate
apps in their own directories with their own urls, models, views etc on
the basis of some sort of standalone functionality.

My own take on this is to not worry when you are a beginner because it
will become obvious when things are getting messy and you suddenly
realise all you have to do is unclutter directories into a nice and
comfortable hierarchy.

One example above is flatviews.py which I said to put in the <project>
directory. If you decided that was getting too cluttered you could make
a views directory and move it into <project>/views/flatviews.py and
refactor urls.py so it called 'views.flatviews.flatpage'

Good luck

Mike

Alex Hall

unread,
Jul 19, 2011, 10:17:11 AM7/19/11
to django...@googlegroups.com
Thanks for all the replies.

1. I did plan to have a dynamic homepage; its most complex section
will be to display the most recent ten additions to the articles
table. It sounds like I can just create a views directory inside my
site directory and use that. I was hesitant to do this simply because
django didn't create one, and it seems to create what I should use.
That is, if it didn't give me a views folder, I figured there was some
other way to do it than to use a views folder.

2. I will keep my app directory structure for now and modify as necessary.

> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.

Reply all
Reply to author
Forward
0 new messages