Django -外部URL链接

eh57zj3b  于 2023-05-01  发布在  Go
关注(0)|答案(1)|浏览(161)

我是Python和Django的新手,我刚刚创建了一个网站。基本上我想有一个网页上显示我们公司的合作伙伴。我已经创建了一个应用程序“合作伙伴”,在模型中,我有“网站”作为字段之一。
在合作伙伴html文件中,我有一个按钮,用户可以点击,这将带他们到合作伙伴的网站在一个新的标签。
我尝试用以下方式链接网站:{{ www.example.com }}然而,这最终是这样的: www.example.com
我只想要合作伙伴网站( www.example.com )在新标签页中打开。
任何帮助感激不尽。如果已经有其他文章,请重新定向。
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'),
]
r7xajy2e

r7xajy2e1#

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

{% for value in partners %}

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

{% endfor%}

如果网站已经与协议一起保存在数据库中,则:

{% for value in partners %}

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

{% endfor%}

......工作完美。

相关问题