html & python connection problem with hyperlinks

justin walters walters.justin01 at gmail.com
Wed May 25 11:06:27 EDT 2016


On Wed, May 25, 2016 at 3:24 AM, <litssa2005 at gmail.com> wrote:

> Why not created the field title, that located on the template
> BusinessList.html as a link to go to Business_Detail.html..? please check
>
> Code:
>
> models. py:
>
> from django.db import models
>
>
> REGIONS = (
> ('ΘΕΣ', 'ΘΕΣΣΑΛΟΝΙΚΗ'),
> ('ΣΕΡ', 'ΣΕΡΡΕΣ'),
> ( 'ΑΘΗ', 'ΑΘΗΝΑ'),
>
>
>
> TYPEOFBUSINESS = (
> ('ΕΣΤ', 'ΕΣΤΙΑΤΟΡΙΑ'),
> ('ΦΑΡ', 'ΦΑΡΜΑΚΕΙΑ'),
> ('ΒΙΒ', 'ΒΙΒΛΙΟΠΩΛΕΙΑ'),
> ( 'ΚΟΜ', 'ΚΟΜΜΩΤΗΡΙΑ'),
> ('ΣΙΝ', 'ΣΙΝΕΜΑ')
>
> )
>
> class Business(models.Model):
> created_Date = models.DateTimeField(auto_now_add=True)
> owner = models.ForeignKey('auth.User', related_name='snippets', null=True)
> title = models.CharField(max_length=100, blank=True, default='')
> Type_of_Business = models.CharField(max_length=3, choices=TYPEOFBUSINESS)
> region = models.CharField(max_length=3, choices=REGIONS)
> address = models.CharField(max_length=100, blank=True, default='')
> phone = models.CharField(max_length=15, blank=True, default='')
> image = models.ImageField(null=True)
>
>
> def __str__(self):
> return str(self.title)
>
> views.py
>
> from django.contrib.auth.models import User
> from django.http import HttpResponse
> from django.shortcuts import render, get_object_or_404
> from rest_framework import filters
> from rest_framework import generics
> from rest_framework import permissions
> from snippets.permissions import IsOwnerOrReadOnly
> from snippets.serializers import SnippetSerializer
> from snippets.serializers import UserSerializer
> from .models import Business
>
>
>
> class UserList(generics.ListAPIView):
> queryset = User.objects.all()
> serializer_class = UserSerializer
>
>
> class UserDetail(generics.RetrieveAPIView):
> queryset = User.objects.all()
> serializer_class = UserSerializer
>
> class BusinessList(generics.ListCreateAPIView):
>
> permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
> queryset = Business.objects.all()
> serializer_class = SnippetSerializer
> filter_backends = (filters.DjangoFilterBackend,filters.SearchFilter,
> filters.OrderingFilter,)
> filter_fields = ('Type_of_Business', 'region')
> search_fields = ('Type_of_Business', 'region')
> ordering_fields = ('Type_of_Business','title', 'region')
>
>
> def BusinessList(request):
> business = Business.objects.all();
> return render(request, 'snippets/BusinessList.html' {'business':business})
>
> def perform_create(self, serializer):
> serializer.save(owner=self.request.user)
>
>
>
> class Business_Detail(generics.RetrieveUpdateDestroyAPIView):
> permission_classes = (permissions.IsAuthenticatedOrReadOnly,
> IsOwnerOrReadOnly,)
> queryset = Business.objects.all()
> serializer_class = SnippetSerializer
>
>
> def Business_Detail(request, pk):
> business = get_object_or_404(Business, pk=pk)
> return render(request, 'snippets/Business_Detail.html', {'business':
> business})
>
> serializers.py
>
> from rest_framework import serializers
> from snippets.models import Business
> from django.contrib.auth.models import User
>
>
> class SnippetSerializer(serializers.HyperlinkedModelSerializer):
> owner = serializers.ReadOnlyField(source='owner.username')
>
> class Meta:
> model = Business
> fields = ('created_Date', 'owner', 'title','Type_of_Business', 'region',
> 'address', 'phone', 'image')
>
>
> class UserSerializer(serializers.ModelSerializer):
> snippets = serializers.PrimaryKeyRelatedField(many=True,
> queryset=Business.objects.all())
>
> class Meta:
> model = User
> fields = ('id', 'username', 'snippets')
>
> BusinessList.html
>
> {% extends 'snippets/base.html' %}
>
> {% block content %}
> {% for business in business%}
> <div class="business">
> <div class="date">
> {{ business.created_Date }} #τυπωσε ημερ.δημιουργιας του Business
> </div>
> <h1> <a href='{% url 'Business_Detail' pk=business.pk %}'>{{
> business.title }}</a></h1>
> <p>{{business.Type_of_Business }}</p>
> <h2>{{ business.region }} </h2>
> <h3>{{ business.address }} </h3>
> <h4>{{ business.phone }} </h4>
> {% if business.image %}
> <img src="{{ business.image.url }}"/>
> {% endif %}
> </div>
> {% endfor %}
> {% endblock %}
>
> Business_Detail.html
>
> {% extends 'snippets/base.html' %}' %}
>
> {% block content %}
> <div class="business">
> {% if business.created_Date %} # αν υπαρχει ημερομηνια δημιουργίας
> <div class="date">
> {{ business.created_Date }}
> </div>
> {% endif %}
> <h1>{{ business.title }} </h1>
> <h2>{{ business.region }} </h2>
> <h3>{{ business.Type_of_Business}} </h3>
> <h4>{{ business.phone }} </h4>
> <p>
> {% if business.image %} # αν υπαρχει εικονα
> <img src="{{ business.image.url }}"/> # παρε εικονα απο το αντιστοιχο url
> {% endif %}
> </p>
> </div>
> {% endblock %}
>
> tutorial/snippets/urls.py
>
> from django.conf.urls import url, include
> from django.contrib import admin
> from rest_framework.urlpatterns import format_suffix_patterns
> from django.contrib.staticfiles.urls import staticfiles_urlpatterns
> from django.conf.urls import include
>
> from . import views
>
> urlpatterns = [
> url(r'^admin/', include(admin.site.urls)),
> url(r'^$', views.BusinessList.as_view()),
> url(r'^business/(?P<pk>[0-9]+)/$', views.Business_Detail.as_view()),
> url(r'^users/$', views.UserList.as_view()),
> url(r'^users/(?P<pk>[0-9]+)/$', views.UserDetail.as_view()),
> url(r'^api-auth/', include('rest_framework.urls',
> namespace='rest_framework')),
>
> ]
>
> urlpatterns = format_suffix_patterns(urlpatterns)
> urlpatterns += staticfiles_urlpatterns()
>
> tutorial/urls.py
>
> from django.conf.urls import include, url
> from django.contrib import admin
> from django.conf import settings
> from django.contrib.staticfiles.urls import staticfiles_urlpatterns
> from django.conf.urls.static import static
>
>
>
> urlpatterns = [
> url(r'^admin/', include(admin.site.urls)),
> url(r'', include('snippets.urls')),
>
> ]
>
> urlpatterns += staticfiles_urlpatterns()
> urlpatterns += static(settings.PHOTO_URL,
> document_root=settings.PHOTO_ROOT)
> --
> https://mail.python.org/mailman/listinfo/python-list
>




There are two problems here. You need to define a namespace for
snippets.urls in tutorials/urls.py:

url(r'', include('snippets.urls', namespace='snippets'))

You also need to give a name to your detail view in snippets/urls.py. Also,
you do not need to call 'as_view() on function based views like business
detail:

url(r'^business/(?P<pk>[0-9]+)/$, views.business_detail,
name='business_detail')

Then, in your list template you can do the following:

<h1><a href="{% url 'snippets:business_detail' pk=business.pk
%}">{{business.title}}</a></h1>

This will create a linked h1 tag that links to the business detail page.

Hope this helps.



More information about the Python-list mailing list