1

I am new to Python and Django, and I have just created a website. Basically I would like to have a page on the website that displays our company partners. I have created an app 'partners' and in the model, I have 'website' as one of the fields.

On the partners html file, I have a button that users can click and this will take them to the partner's website in a new tab.

I tried to link the website in the following way: {{ partner.website }} However this ends up like: www.mydomain.com/partners/www.partnerwebsite.com

I just want the partner website (www.partnerwebsite.com) to open in a new tab.

Any help appreciated. If there is already another post on this, please redirect me.

views.py

from django.shortcuts import render, redirect
from .models import Partner

def index(request):
    partners = Partner.objects.order_by('-date_added').filter(is_published=True)

     context = {
    'partners': partners,
}

     return render(request, 'partners/partners.html', context)

models.py

from django.db import models
from datetime import datetime

class Partner(models.Model):
    name = models.CharField(max_length=200)
    description = models.TextField()
    website = models.CharField(max_length=100)
    email = models.CharField(max_length=200)
    contact_person = models.CharField(max_length=200)
    phone = models.CharField(max_length=100)
    mobile = models.CharField(max_length=100)
    address = models.CharField(max_length=200)
    photo_main = models.ImageField(upload_to='photos/partners/%Y/%m/%d/')
    promo_code = models.CharField(max_length=20, blank=True)
    is_published = models.BooleanField(default=True)
    date_added = models.DateTimeField(default=datetime.now, blank=True)
    def __str__(self):
        return self.name

partners.html

{% extends 'base.html' %}

{% block title %} | Partners {% endblock %}

{% block content %}

<section id="showcase-inner" class="py-5 text-white">
     <div class="container">
       <div class="row text-center">
         <div class="col-md-12">
           <h1 class="display-4">Partners</h1>
        </div>
      </div>
    </div>
  </section>

  <!-- Breadcrumb -->
  <section id="bc" class="mt-3">
    <div class="container">
      <nav aria-label="breadcrumb">
        <ol class="breadcrumb">
          <li class="breadcrumb-item">
            <a href="{% url 'index' %}">
              <i class="fas fa-home"></i> Home</a>
          </li>
          <li class="breadcrumb-item active"> Partners</li>
        </ol>
      </nav>
    </div>
  </section>

<!-- Partners -->
<section id="partners" class="py-4">
    <div class="container">
      <div class="row">
            {% if partners %}
                {% for partner in partners %}

                <!-- Partner 1 -->
                <div class="col-md-6 col-lg-6 mb-4">
                    <div class="card listing-preview">
                    <img class="card-img-top-project" src="{{ partner.photo_main.url }}" alt="">
                        <div class="card-body">
                            <div class="listing-heading text-center">
                                <h4 class="text-primary">{{ partner.name | title }}</h4>
                                <p><i class="fas fa-map-marker text-secondary"></i> &ensp; {{ partner.address }}</p>
                                </div>
                            <hr>
                            <div class="row py-2 text-secondary">
                                <div class="col-12 text-center">
                                    {{ partner.description }}
                                </div>   
                            </div>    
                            {% if partner.promo_code %}
                            <hr>
                            <div class="row py-2 text">
                                <div class="col-12 text-center">
                                    Use the following code to obtain 10% discount: {{ partner.promo_code }}
                                </div>   
                            </div>   
                            {% endif %}                          
                            <div class="container">
                            <hr>
                                <a href="{{ partner.website }}" target="_blank"><button class="btn btn-secondary btn-block">Visit Website</button></a> 
                            </div> 
                        </div>
                    </div>
                </div>
            {% endfor %}
        {% else %}
            <div class="container">
                <p><h5 class="text-center">No Partners Available</h5></p> 
            </div>
        {% endif %}

      </div>
    </div>
  </section>

{% endblock %}

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('',views.index, name='partners'),
]
pH4nT0M
  • 175
  • 2
  • 10
  • share your views.py and models.py ? – Jai Mar 04 '20 at 03:35
  • from django.shortcuts import render, redirect from .models import Partner # Create your views here. def index(request): partners = Partner.objects.order_by('-date_added').filter(is_published=True) context = { 'partners': partners, } return render(request, 'partners/partners.html', context) – pH4nT0M Mar 04 '20 at 04:47
  • instead of pasting it in the comments, edit your question and add the views.py code – Jai Mar 04 '20 at 04:48
  • Sorry I just did. – pH4nT0M Mar 04 '20 at 04:52
  • 1
    It becomes a relative to current domain URL because your `website` value does not start with protocol. – Ivan Starostin Mar 04 '20 at 06:30
  • 1
    So you mean if I add http:// or https:// to the website value for each partner, that will resolve the problem? – pH4nT0M Mar 04 '20 at 07:32
  • 1
    Yes this solved it! Thank you very much! – pH4nT0M Mar 04 '20 at 14:40
  • 1
    It also worth noting that If the website field in the database is saved complete with a protocol i.e. http://www.externalsite.com then it works fine without including the protocol again in the django tags in the template. – kelvinmacharia254 Apr 21 '23 at 06:06

1 Answers1

5

I was able to deduce the below solution from your views file. Add the below code in your template "partners.html"

{% for value in partners %}

<a href="https://{{ value.website }}" target="_blank"><button>my button </button></a>

{% endfor%}

if the website is already saved with a protocol in the database then:

{% for value in partners %}

<a href="{{ value.website }}" target="_blank"><button>my button </button></a>

{% endfor%}

... works perfectly.

kelvinmacharia254
  • 117
  • 1
  • 3
  • 12
Jai
  • 819
  • 7
  • 17
  • Hi Jai, this is the code that I have in my html file: Visit Website – pH4nT0M Mar 04 '20 at 05:01
  • It opens in a blank tab but the link is wrong. It is like I mentioned in the question. (www.mydomain.com/partners/www.partnerwebsite.com) – pH4nT0M Mar 04 '20 at 05:05
  • In that case try replacing partners in views.py with "partners = Partner.objects.all()" tell me if it works – Jai Mar 04 '20 at 05:10
  • Nope still same issue. Maybe something must be added to that view? In the context. I am not sure what though. Or add a new view? – pH4nT0M Mar 04 '20 at 05:37
  • Basically the view is redirecting to www.mydomain.com/partners/. Is there a way to redirect to nothing? As in whatever the value of {{partner.website}} is, becomes the link. – pH4nT0M Mar 04 '20 at 05:40
  • add your "partners.html" to the question, Now i am trying to recreate your issue, it will be helpful – Jai Mar 04 '20 at 05:42
  • I just added it – pH4nT0M Mar 04 '20 at 05:47
  • yes we can change the url redirect, for that u need to add your urls.py to the question. More over I was able to recreate your program without any issues and can conclude that there is no issue with your views,model and .html file. – Jai Mar 04 '20 at 06:18
  • I have added my urls.py file. – pH4nT0M Mar 04 '20 at 07:21
  • It seems we just need to add the protocol to the link for it to work. Thank you very much for your help though! Really appreciate it! – pH4nT0M Mar 04 '20 at 14:41