简体   繁体   中英

Django DetailView Not Getting Data From Model

I'm using Django 1.8 and Python version 2.7.3

I was following this tutorial as I'm still quite new to Django.

I was able to make the sample site from the tutorial and decided to make my own site using the functions from the tutorial. The site I am making is a simple search site for handymen. My issue is I cannot get DetailView to pull information from the database, I can however get it to pull the information when I put it as ListView and call the information in a for loop. I've gone over both the tutorial files and the files for the site I am making as this works perfectly on the tutorial site, hopefully I'm just skipping over something simple.

Here are my files:

urls.py :

from django.conf.urls import patterns, include, url
from django.views.generic import ListView, DetailView
from search.models import People

urlpatterns = patterns('',
        url(r'^$', ListView.as_view(
            queryset=People.objects.all(),
            template_name="search.html")),

        url(r'^(?P<pk>\d+)$', DetailView.as_view(
            model=People,
            template_name="single.html")),

        url(r'^results/$', ListView.as_view(
            queryset=People.objects.all().order_by("-name")[:5],
            template_name="results.html")),         

)

models.py

from django.db import models

# Create your models here.
class People(models.Model):
    name = models.TextField()
    occupation = models.TextField()
    contact = models.TextField()

    def __unicode__(self):
        return self.name

views.py

from django.shortcuts import render
# Create your views here.

base.html

{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<!--header file-->
 <head>
    <title>Handyman Search</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="{% static "css/bootstrap.min.css" %}">
<!-- Optional theme -->
<link rel="stylesheet" href="{% static "css/bootstrap-theme.min.css" %}">

  </head>
{% block content %}
{% endblock %}

search.html

{% extends "base.html" %}
{% load staticfiles %}
{% block content %}
<!--start of body-->
<div class="container" align="center" style="padding-top:180px">
<img src="{% static "images/hammer.jpeg" %}" class="img-responsive">
</div>
<div class="container" style="padding-top:50px">
  <div class="row">
  <div class="col-lg-10 col-sm-push-1">
<form role="search" action="results">
    <div class="input-group">
      <input type="text" class="form-control" placeholder="Search for..." id="searchquery">
      <span class="input-group-btn">
        <button class="btn btn-primary" type="submit" href="results">Search</button>
      </span>
    </div><!-- /input-group -->
</form>
  </div><!-- /.col-lg-6 -->
</div><!-- /.row -->
</div><!--container-->
</body>
<!-- end of body file -->
</html>
{% endblock %}

results.html

{% extends "base.html" %}
{% load staticfiles %}
{% block content %}
<!--start of body-->

<div class="container">
  <div class="row">
<div class="col-xs-1" style="min-height:80px; min-width:90px">
<img src="{% static "images/hammer.jpeg" %}" class="img" width="100%" style="min-height:80px; min-width:80px">
</div>
  <div class="col-lg-10" style="padding-top:20px">
    <div class="input-group">
      <input type="text" class="form-control" placeholder="Search for...">
      <span class="input-group-btn">
        <button class="btn btn-primary" type="button">Search</button>
      </span>
    </div><!-- /input-group -->
  </div><!-- /.col-lg-6 -->
</div><!-- /.row -->
</div><!--container-->

<!--results-->
<div class="container fluid" style="padding-top:80px">
<div class="row">
<div class="span6" style="float: none; margin: 0 auto;">
<div class="panel panel-primary">
  <!-- Default panel contents -->
  <div class="panel-heading">Results</div>


  <!-- List group -->
  <ul class="list-group">
    {% for post in object_list %}
    <li class="list-group-item"><a href="/search/{{post.id}}">{{ post.occupation }}</a></li>
    {% endfor %}
  </ul>
</div>
</div>
</div>
</div>
<!--results end-->
{% endblock %}

single.html

{% extends "base.html" %}
{% load staticfiles %}
{% block content %}
<!--start of body-->

<div class="container">
  <div class="row">
<div class="col-xs-1" style="min-height:80px; min-width:90px">
<img src="{% static "images/tux.jpeg" %}" class="img" width="100%" style="min-height:80px; min-width:80px">
</div>
  <div class="col-lg-10" style="padding-top:20px">
    <div class="input-group">
      <input type="text" class="form-control" placeholder="Search for...">
      <span class="input-group-btn">
        <button class="btn btn-primary" type="button">Search</button>
      </span>
    </div><!-- /input-group -->
  </div><!-- /.col-lg-6 -->
</div><!-- /.row -->
</div><!--container-->

<!--results-->

<div class="container fluid" style="padding-top:80px">
<div class="row">
<div class="span6" style="float: none; margin: 0 auto;">
<div class="panel panel-primary">
  <!-- Default panel contents -->

  <div class="panel-heading">  {{ post.name }} </div>
  <div class="panel-body">
    <p>{{ post.occupation }} 
<br/><br/><br/><br/><br/></p>
<p style="text-align:center"><small><a href="#">{{ post.contact }}</a></small></p>
  </div>
</div>
</div>
</div>
</div>
<!--results end-->
{% endblock %}

When I searched for this particular issue I came across a lot of people posting up views.py, but I rewatched the tutorial and I never edited views.py yet it worked in the tutorial so I'm not 100% sure what went wrong.

Really appreciate any help or if you need anymore to look at please let me know.

You're not passing anything called "post" to your detail template. Django will use the lower-case name of the model by default, so you can either change your template to use "people", or define context_object_name in the view/url to something more appropriate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM