Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. view.py
  2. from django.shortcuts import render
  3. from .models import Pizza
  4.  
  5.  
  6. def index(request):
  7. return render(request, 'pizzaz/index.html')
  8.  
  9.  
  10. def pizza(request):
  11. pizza = Pizza.objects.order_by('name')
  12. context = {'pizza': pizza}
  13. return render(request, 'pizzaz/pizza.html', context)
  14.  
  15.  
  16. def topping(request, topping_id):
  17. topping = Pizza.objects.get(id=topping_id)
  18. entries = topping.topping_set.order_by('topping')
  19. context = {'topping': topping, 'entries': entries}
  20. return render(request, 'pizzaz/topping.html', context)
  21.  
  22. urls.py
  23.  
  24. from django.conf.urls import url
  25. from . import views
  26.  
  27. urlpatterns = [
  28. url(r'^$', views.index, name='index'),
  29. url(r'^pizza/$', views.pizza, name='pizza'),
  30. url(r'^pizza/(?P<topping_id>\d+)/$', views.topping, name='topping')
  31. ]
  32. ------------------------------------------------------------------------------
  33. models.py
  34.  
  35. from django.db import models
  36.  
  37. class Pizza(models.Model):
  38. name = models.CharField(max_length=50)
  39.  
  40. def __str__(self):
  41. return self.name
  42.  
  43. class Topping(models.Model):
  44. pizza = models.ForeignKey(Pizza, on_delete=models.CASCADE)
  45. name = models.TextField()
  46.  
  47. class Meta:
  48. verbose_name_plural = 'topping'
  49.  
  50. def __str__(self):
  51. return self.name
  52. ----------------------------------------------------------------------------------
  53. topping.html
  54. {% extends "pizzaz/base.html" %}
  55.  
  56. {% block content %}
  57. <p>Pizza: {{ pizza }}</p>
  58.  
  59. <p>ingredients:</p>
  60. <ul>
  61. {% for entry in entries %}
  62. <li>
  63. <p>{{ entry.name|linebreaks }}<p>
  64. </li>
  65. {% empty %}
  66. <li>
  67. There are no ingredients for this pizza
  68. </li>
  69. {% endfor %}
  70. </ul>
  71.  
  72. {% endblock content %}
  73. ----------------------------------------------------------------------------------
  74. pizza.html
  75. {% extends "pizzaz/base.html" %}
  76.  
  77. {% block content %}
  78. <p>Pizza Names</p>
  79. <ul>
  80. {% for p in pizza %}
  81. <li>
  82. <a href="{% url 'pizzaz:topping' topping.id %}">{{ p }}</a>
  83. </li>
  84. {% empty %}
  85. <li>No pizzaz have been added</li>
  86. {% endfor %}
  87. </ul>
  88. {% endblock content %}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement