Dear James, Gath, and others,

Thanks for your help!

I think I have the problem mostly solved by now. Basically, I didn't know
how to add a view to the urls.py file when I made a new view in an app. I
think I have it figured out now. :) Originally I was doing something like
this in urls.py:

urlpatterns = patterns( 'members.views',
                       ( r'', 'view1' ),
                       ( r'', 'view2' ),
                       )

and only the first view was executing. Eventually I figured out that I had
to make a new url pattern to make the second view execute, like this:

urlpatterns = patterns( 'members.views',
                       ( r'', 'view1' ),
                       ( r'new url pattern', 'view2' ),
                       )

That said, is it possible to have multiple views assigned to one url
pattern? It looks like this has already been asked on this listserv, and
indeed elsewhere on the web, and the general consensus, I gather, is no:
only one view per url pattern.

Is there any fancy work around I'm missing? :)

The views I was dealing with were pretty simple - just
HttpResponse('Hey!')type stuff. Actually, I'll just paste them here.
If you're looking at the
tutorial, this in the views.py file of the `members` app:

def myFirstView( request ):
    return HttpResponse( "Hey, world!" )

def membersList( request ):
    result = ""
    for m in Member.objects.all():
        result += "%s --- study: %s<br />" % ( m, m.study )
    return HttpResponse( result )

When I was working through this for the first few times, I was hoping to see
"Hey, world!" followed by the list of members, all on one page. Instead, all
that showed up was "Hey, world!", and no members. If I changed the order in
which I added the views in the patterns() call, then I saw the list of
members, but no "Hey..."

So, in the context of this tutorial, is there a way to make both of those
messages appear when I go to the "http://127.0.0.1:8000/members/"; address?

What I eventually did was I changed urlpatterns to be like this:

urlpatterns = patterns( 'members.views',
                       ( r'^$', 'myFirstView' ),
                       ( r'^list$', 'membersList' ),
                       )

which works just fine. But, what I'd like to see is both HttpResponse()
messages on the screen when I navigate to the '^$' url pattern.

I tried the following work-arounds or fixes:

1) make a single view that combines the two. For example, what if I have the
following:

def view1( response ):
    return HttpResponse( 'Django is fun!' )

def view2( response ):
    return HttpResponse( 'I like turtles.' )

and I want my web site to print both of those messages on the screen. Pretty
basic… nothing fancy. I tried combining the functions, like this:

def allViews( request ):
    return view1( request ) + view2( request )

which was an abysmal failure, probably due to how the return values for
HttpResponse() work. If I learned more about that, I could probably combine
the messages in an intelligent manner and get what I want. Is this worth
playing around with, or is there a simpler way to just have both messages
print?

2) Try messing around with the patterns() input. What about this in urls.py:

urlpatterns = patterns( 'members.views',
                       ( r'', 'view1', 'view2' ),
                       )

Whoa, that definitely doesn't work! Haha.. Again, bear with me… I've just
started learning how all these functions work.

Anyway, here's the source code for the tutorial project, excluding any
default stuff that Eclipse makes when you make a django project. Basically
the tutorial tells you to make a Django project in Eclipse named
`djangolesson`. Inside it you make an app named `members` with some models
and views, a database to store instances of those models, and then some more
views that do stuff with the database. Along the way it shows you how to
edit other files like settings.py, urls.py, etc to make it all work.

Files inside `djangolesson`:

============ settings.py: ==============

change INSTALLED_APPS to also include
    'djangolesson.members',
and
    'django.contrib.admin',
like this:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'djangolesson.members', # add this one
    'django.contrib.admin', # and this
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
 )

============ urls.py ==============

from django.conf.urls.defaults import *

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

# Notes: this code makes sure that any url starting with 'appName' will be
processed in the urls.py files of the app named 'appName'. These urls.py
files don't exist automatically;  we have to create one manually whenever we
make a new app.

urlpatterns = patterns( '',
    ( r'^$', include( 'home.urls' ) ),
    ( r'^members/', include( 'members.urls' ) ),
    ( r'^testApp1/', include( "testApp1.urls" ) ),
    ( r'^anotherapp/', include( "anotherapp.urls" ) ),
    ( r'^admin/', include( admin.site.urls ) ),

    # Examples:
    # url(r'^$', 'djangolesson.views.home', name='home'),
    # url(r'^djangolesson/', include('djangolesson.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    # url(r'^admin/', include(admin.site.urls)),
 )
============================================

I'm pretty sure that's all you need to change inside the project files.

Then, for the `members` app,

============ models.py (members) ==============
from django.db import models

class Study( models.Model ):
    name = models.CharField( max_length = 255 )
    year = models.SmallIntegerField()

    def __unicode__( self ):
        return self.name

class Member( models.Model ):
    first_name = models.CharField( max_length = 255 )
    last_name = models.CharField( max_length = 255 )
    street = models.CharField( max_length = 255 )
    nr = models.IntegerField()
    postal_code = models.IntegerField()
    city = models.CharField( max_length = 255 )
    study = models.ForeignKey( Study )

    def __unicode__( self ):
        return "%s, %s" % ( self.last_name, self.first_name )

============ urls.py (members) ==============

from django.conf.urls.defaults import *

urlpatterns = patterns( 'members.views',
                       ( r'^$', 'myFirstView' ),
                       ( r'^list$', 'membersList' ),
                       ( r'^formView$', 'formView' ),
                       )

============ views.py (members) ==============

from django.http import HttpResponse
from models import Member
from django.shortcuts import render_to_response
from djangolesson.members.forms import NameForm

def myFirstView( request ):
    return HttpResponse( "Hey William :)" )

def membersList( request ):
    result = ""
    for m in Member.objects.all():
#        result += "%s" % m.first_name
        result += "%s --- study: %s<br />" % ( m, m.study )
#    result = "test"
    return HttpResponse( result )

def formView( request ):
    f = NameForm( request.GET )
    if f.is_valid():
        return render_to_response( "members/welcome.html", {"name":
f.getName()} )
    return render_to_response( "members/form.html", {"form": f} )

============================================

And, lastly, HTML template files:

============ form.html (templates/members/) ==============
<!doctype html>
<html>
    <head>
        <title>Members List:</title>
    </head>
    <body>
        <form method="get" action="">
            {{ form.as_p }}
            <input type="submit" value="enter" />
        </form>
    </body>
</html>

NOTE: This is where I found either a typo in the tutorial or my doing
something incorrect (probably the latter). In the tut, the action tag in
<form> is like this:
        <form method="get" action=".">
with a period in quotes. With the template like that, I was getting an error
page whenever I submitted the form. I took out the period, so it's just
empty quotes, and it works perfectly:
        <form method="get" action="">

============ welcome.html (templates/members/) ==============
<!doctype html>
<html>
    <head>
        <title>Welcome!</title>
    </head>
    <body>
        Welcome, {{ name }}!
    </body>
</html>

============ list.html ==============
<!doctype html>
<html>
    <head>
        <title>Members List:</title>
    </head>
    <body>
        {% for m in members %}
            {{ m }} --- study: {{ m.study }} <br />
        {% endfor %}
    </body>
</html>

============================================

Thanks again for your time,

William

--

William C Grisaitis
Duke <http://duke.edu/> Physics <http://phy.duke.edu/> 2012
f <http://facebook.com/william.claiborne> |
t<http://twitter.com/caravaggisto> |
(321) 422 - 9231



On Mon, Jul 25, 2011 at 12:17 AM, Gath <pgath...@gmail.com> wrote:

> What's the problem you having?
>
> On Jul 25, 12:48 am, William C Grisaitis <wgrisai...@gmail.com> wrote:
> > Hi all,
> >
> > I'm new to Django and started following a tutorial I found on google,
> named
> > "djangoles-tutorial.pdf", which shows you how to make a very basic Django
> > web app with Eclipse and PyDev. It's the first hit on google if you
> search
> > "django tutorial filetype:pdf".
> >
> > Does anyone here have experience with this tutorial? I'm having a minor
> > problem in section 5, `Using Models in Views` (p. 6 of the pdf). I've
> adding
> > a function to the `views.py` file of an app, named `members`, inside the
> > django project.
> >
> > If anyone has used this tutorial, let me know and I'll post some code!
> >
> > Again, this is all on Eclipse (3.6.2), PyDev, and Django 1.3.
> >
> > Thanks and best,
> >
> > William
> >
> > --
> >
> > William C Grisaitis
> > Duke <http://duke.edu/> Physics <http://phy.duke.edu/> 2012
> > f <http://facebook.com/william.claiborne> |
> > t<http://twitter.com/caravaggisto> |
> > (321) 422 - 9231
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>

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

Reply via email to