0

我是 Python 和 Django 的新手,我刚刚创建了一个网站。基本上我想在网站上有一个页面显示我们公司的合作伙伴。我创建了一个应用程序“合作伙伴”,在模型中,我将“网站”作为字段之一。

在合作伙伴 html 文件中,我有一个用户可以单击的按钮,这会将他们带到新选项卡中的合作伙伴网站。

我尝试通过以下方式链接该网站:{{ partner.website }} 但是最终结果如下:www.mydomain.com/partners/www.partnerwebsite.com

我只希望合作伙伴网站 (www.partnerwebsite.com) 在新标签页中打开。

任何帮助表示赞赏。如果已经有另一个帖子,请重定向我。

视图.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)

模型.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

合作伙伴.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 %}

网址.py

from django.urls import path
from . import views

urlpatterns = [
    path('',views.index, name='partners'),
]
4

1 回答 1

3

我能够从您的视图文件中推断出以下解决方案。在您的模板“partners.html”中添加以下代码

{% for value in partners %}

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

{% endfor%}

于 2020-03-04T04:58:03.990 回答