Форум сайта python.su
начал новый сайт делать для работы, решил сделать динамическую менюшку чтобы все описания и url'ы брались из базы и в админке можно было их менять/добавлять и т.д.
models.py
class Menu(models.Model):
url = models.CharField(max_length=255, unique=True)
name = models.CharField(max_length=255)
class Submenu(models.Model):
menuparent = models.ForeignKey(Menu)
url = models.CharField(max_length=255, unique=True)
name = models.CharField(max_length=255)
from django.conf.urls.defaults import *
from django.contrib.auth.views import login, logout
from django.conf import settings
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^ipoteka/', include('ipoteka.foo.urls')),
# Uncomment the admin/doc line below and add 'django.contrib.admindocs'
# to INSTALLED_APPS to enable admin documentation:
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
url(r'', include('ipoteka.website.urls')),
)
# -*- coding: utf-8 -*-
from django.conf.urls.defaults import *
from website.models import Menu, Submenu
urlpatterns = patterns('ipoteka.website.views',
(r'^$', 'index'),
)
""" меню первого уровня """
menu = Menu.objects.all()
for menuitems in menu:
urlpatterns += patterns('ipoteka.website.views',
url(r'^' + menuitems.url + '/$', menuitems.url),
)
""" меню второго уровня """
submenu = Submenu.objects.all()
for menuitems in menu:
for submenuitems in submenu:
if submenuitems.menuparent.id == menuitems.id:
urlpatterns += patterns('ipoteka.website.views',
url(r'^' + menuitems.url + '/' + submenuitems.url + '/$', submenuitems.url),
)
TemplateSyntaxError at /admin/
Caught an exception while rendering: Tried news in module ipoteka.website.views. Error was: 'module' object has no attribute 'news'
Original Traceback (most recent call last):
File "/usr/lib/python2.5/site-packages/django/template/debug.py", line 71, in render_node
result = node.render(context)
File "/usr/lib/python2.5/site-packages/django/template/defaulttags.py", line 370, in render
url = reverse(self.view_name, args=args, kwargs=kwargs, current_app=context.current_app)
File "/usr/lib/python2.5/site-packages/django/core/urlresolvers.py", line 341, in reverse
*args, **kwargs)))
File "/usr/lib/python2.5/site-packages/django/core/urlresolvers.py", line 275, in reverse
possibilities = self.reverse_dict.getlist(lookup_view)
File "/usr/lib/python2.5/site-packages/django/core/urlresolvers.py", line 195, in _get_reverse_dict
self._populate()
File "/usr/lib/python2.5/site-packages/django/core/urlresolvers.py", line 175, in _populate
for name in pattern.reverse_dict:
File "/usr/lib/python2.5/site-packages/django/core/urlresolvers.py", line 195, in _get_reverse_dict
self._populate()
File "/usr/lib/python2.5/site-packages/django/core/urlresolvers.py", line 187, in _populate
lookups.appendlist(pattern.callback, (bits, p_pattern))
File "/usr/lib/python2.5/site-packages/django/core/urlresolvers.py", line 137, in _get_callback
raise ViewDoesNotExist, "Tried %s in module %s. Error was: %s" % (func_name, mod_name, str(e))
ViewDoesNotExist: Tried news in module ipoteka.website.views. Error was: 'module' object has no attribute 'news'
Request Method: GET
Request URL: http://192.168.16.200:7090/admin/
Exception Type: TemplateSyntaxError
Exception Value:
Caught an exception while rendering: Tried news in module ipoteka.website.views. Error was: 'module' object has no attribute 'news'
Original Traceback (most recent call last):
File "/usr/lib/python2.5/site-packages/django/template/debug.py", line 71, in render_node
result = node.render(context)
File "/usr/lib/python2.5/site-packages/django/template/defaulttags.py", line 370, in render
url = reverse(self.view_name, args=args, kwargs=kwargs, current_app=context.current_app)
File "/usr/lib/python2.5/site-packages/django/core/urlresolvers.py", line 341, in reverse
*args, **kwargs)))
File "/usr/lib/python2.5/site-packages/django/core/urlresolvers.py", line 275, in reverse
possibilities = self.reverse_dict.getlist(lookup_view)
File "/usr/lib/python2.5/site-packages/django/core/urlresolvers.py", line 195, in _get_reverse_dict
self._populate()
File "/usr/lib/python2.5/site-packages/django/core/urlresolvers.py", line 175, in _populate
for name in pattern.reverse_dict:
File "/usr/lib/python2.5/site-packages/django/core/urlresolvers.py", line 195, in _get_reverse_dict
self._populate()
File "/usr/lib/python2.5/site-packages/django/core/urlresolvers.py", line 187, in _populate
lookups.appendlist(pattern.callback, (bits, p_pattern))
File "/usr/lib/python2.5/site-packages/django/core/urlresolvers.py", line 137, in _get_callback
raise ViewDoesNotExist, "Tried %s in module %s. Error was: %s" % (func_name, mod_name, str(e))
ViewDoesNotExist: Tried news in module ipoteka.website.views. Error was: 'module' object has no attribute 'news'
Exception Location: /usr/lib/python2.5/site-packages/django/template/debug.py in render_node, line 81
Python Executable: /usr/bin/python
Python Version: 2.5.2
Python Path: ['/usr/lib/python2.5', '/usr/lib/python2.5/plat-linux2', '/usr/lib/python2.5/lib-tk', '/usr/lib/python2.5/lib-dynload', '/usr/local/lib/python2.5/site-packages', '/usr/lib/python2.5/site-packages', '/usr/lib/python2.5/site-packages/PIL', '/var/lib/python-support/python2.5', '/var/www/djcode', '/var/www/djcode/ipoteka', '/srv/http/djcode', '/srv/http/djcode/ipoteka']
Server time: Чтв, 6 Авг 2009 08:45:54 +0400
id url name
1 about О корпорации
2 crediting Кредитование
3 realty Недвижимость
4 press Пресс-служба
id menuparent_id url name
1 1 direction руководство
2 1 offices офисы
3 1 partners партнеры
4 2 programs ипотечные программы
5 2 documents перечень документов
6 2 howto как получить кредит
7 3 not_ready строящиеся объекты
8 4 news новости
Отредактировано (Авг. 6, 2009 10:50:29)
Офлайн
мдя, понял в чем фишка…
из-за того что в views нету всех представлений из списка submenu.url он ругается.
переписал вот так
""" меню первого уровня """
urlpatterns += patterns('ipoteka.website.views',
url(r'^(?P<menu_url>\w+)/$', 'menufunction'),
)
menu = Menu.objects.all()
menulist = []
for menuitems in menu:
menulist.append(menuitems.url)
def menufunction(request, menu_url):
try:
if menu_url in menulist:
return render_to_response(menu_url + '.html', {'menu': menu, 'submenu': submenu, },
)
except:
raise Http404
Офлайн
Кстати, а зачем две модели, если можно сделать parent = models.ForeignKey('self', null=True)? Или я не так понял?)
Офлайн