Django外部URL链接

2021-12-08 06:12:37 发布

您现在位置:Python中文网/ 问答频道 /正文

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

在partners html文件中,我有一个按钮,用户可以单击该按钮,这将在一个新选项卡中将他们带到partners的网站

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

我只想在一个新选项卡中打开合作伙伴网站(www.partnerwebsite.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 %}

url.py

from django.urls import path
from . import views

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