tags:

views:

47

answers:

1

I am trying to get the following setup up going.

  1. Flatpages: Where all my static sites are (like: about, contact,..)

  2. Dynamic Pages:

Here I am trying to link from one of the Flatpages to a start site:

the regex in the url conf of this startsite I tried was:

(r'^myapp/start/(\d+)/$', 'mysite.views.def_that_should_just_show_hello_world'),

In the views I had:

def def_that_should_just_show_hello_world(request):
 return HttpResponse("Hello experiment world")

If I go to

/myapp/ I get 404: No FlatPage matches the given query. /myapp/start/ I get 404: No FlatPage matches the given query. /myapp/start/1 I get

Exception Type: TypeError def_that_should_just_show_hello_world takes exactly 1 argument (2 given)

I thought with this setup I would get "Hello experiment world" on EVERY page.

Where did I go wrong? I dont understand the multiple sites approach in regexs. What would I have to do to print hello world on all these sites? And then, what would I have to do to display 1 image on all of these sites?

Thanks a lot for the help!

+1  A: 

You regular expression has a matching group in it - the (\d+) bit.

This requires one or more numeric characters to appear at the end of the url for that view. If you do not include the number at the end, this regular expression will not match the url. (url matching works like any other regular expression matching).

When you do include the number, eg. /myapp/start/1 you then have another problem. Because there is a matching group, the part of the url in the brackets will be passed as another argument to your view. Views are always passed the request as their first parameter but in this case the '1' matched by the (\d+) is provided as a second argument. This is why you are hetting the TypeError in this case.

Django's documentation has a lot of information on how url dispatching works, read that through and see if that makes sense!

adamnfish
Thanks! Sorry, but I dont get it. Especially this you wrote:"Because there is a matching group, the part of the url in the brackets (DO YOU MEAN: (\d+)) will be passed as another argument to your view."What can I do that it will pass the correct argument (first, second)?
MacPython
If you add another argument to your view function (or add *args to catch any number of arguments) that view will then match the url `/myapp/start/1`. Have its signature be `def def_that_should_just_show_hello_world(request, number):`.
adamnfish
This allow the view to match urls of the form /myapp/start/<a number> which means it will be called with the request object and the number passed to it. It's not clear to me what your ultimate goal is though - perhaps you could try and clarify?
adamnfish
So my ultimate goal would be to have multiple sites e.g. myapp/start/1-40. Every site should have either 1 image or 1 sound or 1 movie. These should be stored in a list and than displayed each one of these on one page (1-40) with a next button (just like a gallery). The next button has to record the timestamp when it was clicked. Thats it. And it seems to me some basic standard thing to do, but every time I solve one problem another appears.. Thanks for your time.
MacPython
Do i have to use regexes to do urls.py? What would be an option?
MacPython
Get the url matching as I have described and I think things will start to become more clear to you. Change your view to `return HttpResponse(number)` and then try `return HttpResponse(my_list_of_media[number])`. Then try detecting the type of the list item (image, video, sound) and make use a template that displays each appropriately. The view will be run every time the next button is clicked, so you can use that to track times. You're on the right lines now, have a fiddle :)
adamnfish
I added this [0-9]*/ and now I can put any number I want and it returns hello "world". I dont understand exactly why but it works.What you are suggesting right now, sounds very promising. I have to check that. Thanks a million!
MacPython