2016-08-02 3 views
0

저는 지역 보석 상점을위한 데모 웹 사이트를 구축하고 있으며, 보석 브랜드 목록 (ListView)을 만들고 다른 페이지의 설명 (DetailView)에 각각 연결하려고합니다. 나는 내 ListView와 내 DetailView를 함께 연결하려고 노력하면서 15 시간을 보냈으며 아무 것도 고치지 않았다. 이들은 내가 함께 일하고 있어요 도면이다 :이 첫 번째보기를 들어DetailView와 ListView를 함께 연결하려면 어떻게해야합니까?

전망

class BrandView(ListView): 
    template_name = 'products.html' 
    queryset = models.Brand.objects.order_by('brand_name') 
    context_object_name = 'brand_list' 

을, 나는 해당 세부 정보 페이지에 링크로의 검색어에서 각 개체를 표시하는 템플릿을 만든 다음보기로 표현되어야한다 : 나는 또한 일반 함수로 마지막 뷰를 작성하려고했습니다

class TextView(DetailView): 
    template_name = 'brands/brand_text.html'  
    context_object_name = 'brand' 

    def get(self, request, slug): 
     # Grabs the Brand object that owns the given slug 
     brand = models.Brand.objects.get(slug = slug) 

     # renders self.template_name with the given context and model object 
     return render(request, self.template_name, self.context_object_name) 

,하지만이 중 아무것도 달성하지 않습니다

def text_view(request, slug): 
    brand = models.Brand.objects.get(slug = slug) 
    return render(request, 'brands/brand_text.html', {'brand': brand,}) 

기본적으로 ListView에서 개체를 클릭하면 해당 개체의 슬러그가 URL에 추가되지만 페이지는 변경되지 않습니다. 그렇다면 DetailView가 ListView에서 제공된 정보를 가져 오도록 내 두 뷰를 성공적으로 연결할 수 있습니까?


아마도 내 템플릿을 편리하게 증명할 수 있습니다

템플릿

brand_text.html

{% block content %} 
    <div class= "brands" style="animation: fadein 1.5s 1;"> 
      <p> 
       <a class = "nav_link" href="{% url 'products' %}">Back</a> 
      </p> 
    </div> 

    <div class= "brand_descriptions"> 
     <p>{{ brand.description }}</p> 
    </div> 

{% endblock %} 

products.html

{% block content %} 
    <div class= "brands" style="animation: fadein 1.5s 1;"> 
     {% for item in brand_list %} 
      <p> 
       <a class = "nav_link" href="{% url 'brand_text' item.slug %}">{{ item.brand_name }}</a> 
      </p> 
     {% endfor %} 
    </div> 

{% endblock %} 

UPDATE 2016년 8월 2일 :

URL 패턴

url(r'^products/', BrandView.as_view(), name = 'products'), 
url(r'^products/(?P<slug>[-\w]+)', TextView.as_view(), name = 'brand_text'), 

(이것은 내 첫 번째 질문, 그래서 너무 오래 만약 내가 사과!)

+0

당신은 그것을 제안 할 수 있습니다. 목록에 각 iteam을 저장하고 싶습니다. ID가있는 모델과 각각의 ID를 가지고 Details.model에 세부 정보를 저장합니다. iteam의 목록을 보내는 동안 이드와 내가 보여주고 싶은 기본 정보를 보내라.해당 perticular iteam 클릭 한 후 해당 ID를 detailsView 해당 인 iteam 전달할 것이라고 Information.I Ajax 호출을 요청할 수 있도록 내 페이지를 새로 고치지 않습니다. –

+0

URL 패턴을 보여주십시오. – Alasdair

+0

@Alasdair 언급에 감사 드리며, URL 패턴이 표시되도록 게시물을 업데이트했습니다. – GTech

답변

0

귀하의 문제가 귀하의 URL 패턴. 당신은 당신의 정규식 끝에서 달러를 놓쳤습니다. 즉, /products/my-slug/BrandView 대신 TextView 대신 정규식과 일치합니다. 당신이 당신의 상세보기를 단순화 할 수

url(r'^products/$', BrandView.as_view(), name = 'products'), 
url(r'^products/(?P<slug>[-\w]+)$', TextView.as_view(), name = 'brand_text'), 

참고 : : 기본 이미 '브랜드'이기 때문에 context_object_name을 설정할 필요가 없습니다

class TextView(DetailView): 
    template_name = 'brands/brand_text.html' 

로 변경합니다. get을 제네릭 클래스 기반보기로 재정의하는 것은 좋지 않습니다.보기의 많은 기능을 잃거나 복제해야합니다.

+0

와우, 빠진 달러 기호가 그것을 할 것이라고 생각하지 마십시오. 고마워요 !! – GTech

관련 문제