0

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!

2 Answers 2

1

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!

Sign up to request clarification or add additional context in comments.

7 Comments

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)?
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):.
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?
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.
Do i have to use regexes to do urls.py? What would be an option?
|
0
from your_app_name import views
from django.conf.urls import url


   urlpatterns = [
       url(r'^$',views.method_name,name ='index'),
       path('admin/', admin.site.urls), 

1 Comment

This answer should also explain how op can use this code, what needs to be changed, and why, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.