2014-09-16 4 views
0

URL에서 뷰에 전달할 변수를 가져올 수 있습니까? 예를 들어. 위치가 많은 위치 테이블이 있습니다. 위치를 클릭하면 해당 위치의 세부 정보 페이지로 이동합니다. 위치 ID가 URL에 추가됩니다. 그런 다음 다른 페이지 /보기에 대한 링크가 있지만 다음보기에서 위치 ID를 참조하려고합니다.Django가 URL에서 뷰로 변수를 가져 오는 중

urls.py :

url(r'^locations/get/(?P<location_id>\d+)/$', 'assessments.views.location'), 
url(r'^locations/get/(?P<location_id>\d+)/next_page/$', 'assessments.views.next_page'), 

view.py :

def location(request, location_id=1): 
    return render_to_response('dashboard/location.html', {'location': Location.objects.get(id=location_id) }) 

def next_page(request): 
    loc = Location.objects.get(id='id of that location') 
+0

()'- location_id 또는 다른 어떤 것? –

+0

다음 페이지로 가기 전에 내가 있었던 위치의 위치가 필요합니다. 그것이 의미가 있는지 확실하지 않습니다. –

+0

'next_page'는 원래'location' 함수와 똑같은'location_id' 인자를 취합니다. 그래서 똑같은 방식으로 사용할 수 있습니다. –

답변

1

단순히 이름과 뷰 기능에

def next_page(request,location_id): 
    loc = Location.objects.get(id=location_id) 
1

OK 액세스, 난 아직 아니에요 확실하게 정확히 무엇을 의미합니까?하지만 다음 두 단편 중 하나가 문제를 해결해야합니다.

  1. 이 URL은 location_id 포함, 그래서 그냥 next_page()에서 사용 :

    def location(request, location_id): 
        return render_to_response('dashboard/location.html', {'location': Location.objects.get(id=location_id) }) 
    
    def next_page(request, location_id): 
        loc = Location.objects.get(id=location_id) 
    
  2. 패스 location_id을 request.session에 :

    당신이 next_page`에 필요합니까 위치
    def location(request, location_id): 
        request.session['selected_location'] = location_id 
        return render_to_response('dashboard/location.html', {'location': Location.objects.get(id=location_id) }) 
    
    def next_page(request): 
        try: 
         selected_location = request.session['selected_location'] 
        except KeyError: 
         # Not sure if 404 is the best status code here but you get the idea 
         raise Http404 
        loc = Location.objects.get(id=selected_location) 
    
+0

일부는 내가 그것이 훨씬 더 복잡하다는 것을 나 자신에게 확신시키는 방법. 감사 –

관련 문제