Thank you for your attention to my problem. Watch me finally do it.
models.py
class WebPage(MPTTModel): slug=RuSlugField(max_length=20,unique=True) title=models.CharField(max_length=50) content=models.TextField() parent=TreeForeignKey('self',null=True,blank=True,related_name='children') class MPTTMeta: order_insertion_by=['slug'] def get_absolute_url(self):
urls.py
urlpatterns = patterns('website.views', url(r"^add/$", "add_page",name="add"), url(r"^(?P<full_slug>.*)/add/$", "add_page",name="add"), url(r"^(?P<full_slug>.*)/edit/$", "edit_page",name="edit"), url(r'^$', ListView.as_view(model=WebPage,template_name='index.html',context_object_name="webpages_list",),name='index'), url(r"^(?P<full_slug>.*)/$", "page", name="page"), )
views.py
def page(request, full_slug): # Make a list from full_slug. # For ex. /trance/progressive_trance/fonarev -> ['trance','progressive_trance','fonarev'] slugs=full_slug.split('/') page=None # Get a page by it slug if len(slugs)>1: page=get_object_or_404(WebPage,slug=slugs[-1])#slugs=['trance','vocal_trance'] -> 'vocal_trance' elif len(slugs)==1: page=get_object_or_404(WebPage,slug=slugs[0])#slugs=['trance'] -> 'trance' # Check if page url matches requested full_slug if page.get_absolute_url().strip('/') == full_slug: return render_to_response('page.html', {'page': page},context_instance=RequestContext(request)) else: raise Http404 def edit_page(request,full_slug): slugs=full_slug.split('/') page=None if len(slugs)>1: page=get_object_or_404(WebPage,slug=slugs[-1]) elif len(slugs)==1: page=get_object_or_404(WebPage,slug=slugs[0]) if not page.get_absolute_url().strip('/') == full_slug: raise Http404 # Send POST data for update an existing page.Update a page. if request.method=='POST': form=WebPageForm(request.POST, instance=page) if form.is_valid(): form.save() return HttpResponseRedirect(page.get_absolute_url()) # Render a form to edit data for existing page else: form=WebPageForm(instance=page) return render_to_response('edit_page.html',{'form':form,},context_instance=RequestContext(request)) def add_page(request,full_slug=None): parent_page=None slug=None if full_slug: slug=full_slug.split('/') # If there is a slug in REQUEST(ex.'trance')->we need to add a new_page to her parent_page. # So get a parent page. if slug: if len(slug)>1: parent_page=get_object_or_404(WebPage,slug=slug[-1]) elif len(slug)==1: parent_page=get_object_or_404(WebPage,slug=slug[0]) # Create a new_page if request.method=='POST': form=WebPageForm(request.POST) if form.is_valid(): new_page=form.save(commit=False) if parent_page: new_page.parent=parent_page new_page.save() return HttpResponseRedirect(new_page.get_absolute_url()) # Return an unbounded form else: form=WebPageForm() return render_to_response('add_page.html',{'form':form,},context_instance=RequestContext(request))
The trick is that we need to check if the page really has access to it through full_slug:
if not page.get_absolute_url().strip('/') == full_slug: raise Http404
Otherwise, it may be wrong if you can only check bullets.