On Tue, 2006-07-11 at 15:30 -0700, Tomas Jacobsen wrote: > Wow, thanks for the help! I do understand the principle of views and > templates now, but I still need some help. > > I have managed to get out the slug name of my projects in > "mydomain/portfolio/". But I don't know how I write the view for the > detail page or how I get the right URL to the detail. > > I want the URL to be "mydomain/portfolio/category_slug/project_slug" > like "mydomain/portfolio/3d/house/". > > My urls.py looks like this:
OK, let's try fixing one problem at a time, instead of all 47 at once. :-) [...] > urlpatterns = patterns('', > # Portfolio: > (r'^portfolio/$', 'myproject.portfolio.views.index'), > (r'^portfolio/(?P<categoy_slug>\d+)/$', > 'myproject.portfolio.views.list'), > (r'^portfolio/(?P<categoy_slug>\d+)/(?P<project_slug>\d+)/$', > 'myproject.portfolio.views.detail'), Let's just focus on this bit. > My portfolio/view.py looks like this: > > from django.shortcuts import render_to_response > > from myproject.portfolio.models import Project > from myproject.portfolio.models import Category > > # Create your views here. > def index(request): > latest_project_list = Project.objects.all().order_by('-created') > return render_to_response('portfolio/index.html', > {'latest_project_list': latest_project_list}) You don't mention what does and doesn't work here, so let's ask a few questions: (1) Firstly, you refer to the view functions as myproject.portfolio.views.* (with an 's' on views), but later on you say the file is called portfolio/view.py. Is this a typo? You are creating Python import paths here, so if your file is called view.py, you can't refer to it as "views" in the import path in urlpattens. (2) Assuming question (1) really is a typo, does accessing the "portfolio/" URL work? From the code you posted, it looks like it should work more or less correctly. If you are not sure if it is being called, drop a print statement in there (if you are using the development server, the print output will come out in the terminal window where the development server is running). The only potential problem is that your template path may or may not be wrong depending upon how you've set up your TEMPLATE_DIR settings. You basically have two choices: (a) Put the templates under a directory in the TEMPLATE_DIR list and then refer to them by their path (remove the "TEMPLATE_DIR" prefix from the front). So if TEMPLATE_DIR = ('/home/malcolm/templates',) and I wanted to refer to my template as 'portfolio/index.html', I could put it in /home/malcolm/templates/portfolio/index.html. (b) Put the templates inside an application directory in a sub-directory called "templates". So you might put it in myproject/portfolio/templates/ in your case. Again, Django will strip off the bit up to and including the "templates/" portion of the path, so you use the rest to refer to the template. If you have a file called myproject/portfolio/templates/portfolio/index.html you are fine here. Either (a) or (b) works. Generally, (b) is a good option for templates that belong to a particular application (since then you can easily move the templates around with the rest of the code), whereas (a) is good for cross-application or site-wide templates (they belong to no particular project or application). Assuming you get this far, you now need to create list() and detail() functions (inside views.py). The list() function, for example, would start off as def list(request, category_slug): # ... the name of the parameter here should match the name you use in the reg-exp to capture that argument (currently it doesn't, but I am assuming you have accidently misspelt "category" in your urls.py and will fix that; but at least note that there is a possibly unexpected typo there). Also, as a stylistic issue, calling a function list() is risky. Python already has a list() builtin so in that module, you have now lost access to Python's list() function, which might cause unexpected problems down the track. If you get this far, then you probably have the hang of things. If you are not getting this far, perhaps showing what errors you are seeing will help. Again, focus on getting one path right at a time and fixing one error at a time. Good luck, Malcolm --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~----------~----~----~----~------~----~------~--~---