0

If i have a url that is "someurl/report/1/" where "report" is an app and "1" corresponds to a certain id of a model in my SQL database, how would i substitute the variables in the template with that specific models data?

In my case I am writing a website that displays a surf report for different beaches. I have set it up so each SQL model id corresponds to a different beach. So if I wanted to use the data of beach "3" in the template, how would i display those in the html template?

TRACEBACK

Using the URLconf defined in surfsite.urls, Django tried these URL patterns, in this order: ^admin/ ^report/ ^$ [name='index'] ^report/ (?P[0-9]+)$ [name='get_report'] The current URL, report/1/, didn't match any of these.

#URLS.PY
from django.conf.urls import url
from django.conf.urls.static import static
from . import views

    urlpatterns = [
        # /index/
        url(r'^$', views.index, name='index'),

        # /report/
        url(r'(?P<beach_id>[0-9]+)$', views.get_report, name='get_report'),
    ]


#MY TEMPLATE
<!DOCTYPE html>
<html>
    {% load staticfiles %}
    <link rel="stylesheet" type="text/css" 
    href="{% static 'report/css/style.css' %}"/>

    <link href="https://fonts.googleapis.com/css?family=Biryani:300,400,800" 
    rel="stylesheet"/>

   <head>
      <title>REALSURF</title>
  </head>

   <body>

      <h1>
        <form id="search">
          <input id="search" type="text">
        </form>
      </h1>

    <div class="container">
      <div class="column">
        <div class="text">Wind</div>
        <div class="number">{{Break.wind}}</div>
      </div>            
      <div class="column">
        <div class="text">Wave Height</div>
        <div class="number">{{Break.low}}-{{Break.high}}</div>
      </div>    
      <div class="column">
        <div class="text">Tide</div>
        <div class="number">{{Break.tide}}</div>
      </div>    
    </div>

    <h2>
      REALSURF
    </h2>

    <h3>
      A simple site by David Owens
    </h3>

   </body>
</html>




#MY MODEL
from __future__ import unicode_literals   
from django.db import models

class Beach(models.Model):
    name = models.CharField(max_length=50)
    high = models.SmallIntegerField()
    low = models.SmallIntegerField()
    wind = models.SmallIntegerField()
    tide = models.DecimalField(max_digits=3, decimal_places=2)

    def __str__(self):
        return self.name


#MY VIEWS
from django.http import Http404
from django.shortcuts import render
from .models import Beach

def index(request):
    allBeaches = Beach.objects.all()
    context = {
        'allBeaches': allBeaches,
    }
    return render(request, 'report/index.html', context) 

def get_report(request, id):
    try:
        beach = Beach.objects.get(id=id)

    except Beach.DoesNotExist:
        raise Htpp404("404")

    return render(request, 'report/index.html', {'beach': beach}) 

1 Answer 1

1

If i understand you right, you want to create view, that render beach separately You can do something like this, your view:

def get_beach(request, id)
  beach = Beach.objects.get(id=id)
  return render(request, 'path/to/your/template', {'beach':beach})

urls:

url(r'^someurl/report/(?P<id>[0-9]+)$', views.get_beach(), name='get_beach'),

template url to this page:

<a href= "someurl/report/{{beach.id}}">beach</a>

Edited Its your view, as i understand, this is detail view, but select all objects(Break.objects.all())

def detail(request, break_id):
    try:
        allBreaks = Break.objects.all()

    except Break.DoesNotExist:
        raise Http404("404")

    return render(request, 'report/index.html', {'allBreaks': allBreaks})

so you have to change this on this:

 def detail(request, break_id):
      try:
        break_detail = Break.objects.get(id=break_id)
        return render(request, 'path/to/your/template', {'break_detail':break_detail})
      except Break.DoesNotExist:
        raise Http404("404")

then your url should look like this:

url(r'^someurl/report/(?P<break_id>[0-9]+)$', views.detail(), name='detail'),

or you can use get_object_or_404:

def detail(request, break_id): break_detail = get_object_or_404(Break, id=break_id) return render(request, 'path/to/your/template', {'break_detail':break_detail})

Url is the same.

So if you want to access to wind field, you just write this tempalte tag {{break_detail.wind}}

UPD2

change place from this

^admin/
^report/ ^$ [name='index']
^report/ (?P<beach_id>[0-9]+)$ [name='get_report']

to this:

^admin/
^report/(?P<beach_id>[0-9]+)$ [name='get_report']
^report/^$ [name='index']

and delete spaces in urls after report/

Sign up to request clarification or add additional context in comments.

8 Comments

the line beach = Beach.objects.get(id=id) is what filters to get the exact beach from the URL correct?
So how would i access the wind variable, for example, in the template?
its hard to answer on your question, because you didn't post you models and views. But if model Beach has field wind, you can access to it {{beach.wind}}. If you want more specific answer you have to post more information.
The model and views are in the bottom of the code section if you scroll down. I am getting a syntax error on the url line you gave when i try to run the server?
now when testing the site, "someurl/report/" works but any url like "someurl/report/1/" fails.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.