不存在匹配查询-2条

时间:2018-08-16 17:05:01

标签: python html css django-models

首先,很抱歉给您带来不便和混乱,我刚刚开始使用Django,这是我的第一篇文章。

发生的事情是,我必须使用2个slugs(slugfrance和slugslovenia),而slugfrance试图使用slugslovenia的数据库(我相信)。当我尝试输入斯洛文尼亚的帖子(= experiences)时,我没有问题,但是对于法国,它告诉我:

  

SloveniaExperience匹配查询不存在。

我的要求是:

dj-database-url==0.5.0
Django==1.8
django-countries==5.3.1
gunicorn==19.9.0
psycopg2==2.7.5
whitenoise==4.0

我的代码如下:

对于模型。py

class SloveniaExperience(models.Model):
        author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
        exchange_country = CountryField(default='DEFAULT VALUE')
        category = models.IntegerField(choices=CATEGORY_CHOICES)
        title = models.CharField(max_length=200, default='DEFAULT VALUE')
        text = models.TextField(default='DEFAULT VALUE')
        slugslovenia = models.SlugField(default='default-value')
        created_date = models.DateTimeField(
                default=timezone.now)
        published_date = models.DateField(
                blank=True, null=True)
        def publish(self):
            self.published_date = timezone.now()
            self.save()
        def __str__(self):
            return self.title

>class FranceExperience(models.Model):
        author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
        exchange_country = CountryField(default='DEFAULT VALUE')
        category = models.IntegerField(choices=CATEGORY_CHOICES)
        title = models.CharField(max_length=200, default='DEFAULT VALUE')
        slugfrance = models.SlugField(default='default-value')
        text = models.TextField(default='DEFAULT VALUE')
        created_date = models.DateTimeField(
                default=timezone.now)
        published_date = models.DateField(
                blank=True, null=True)
        def publish(self):
            self.published_date = timezone.now()
            self.save()
        def __str__(self):
            return self.title

对于urls.py

    urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', views.home, name="home"),
    url(r'^home/$', views.home, name="home"),
    url(r'^experiences-asia/$', views.experiencesasia, name="experiencesasia"),
    url(r'^experiences-europe/(?P<slug>[\w\-]+)/$', views.blogslovenia, name="blogslovenia"),
    url(r'^experiences-europe/(?P<slug>[\w\-]+)/$', views.blogfrance, name="blogfrance"),
    url(r'^experiences-europe/$', views.experienceseurope, name="experienceseurope"),
    url(r'^engineering/$', views.engineering, name="engineering"),
    url(r'^talks/$', views.talks, name="talks"),
    url(r'^about/$', views.about, name="about"),
]

对于views.py:

def blogslovenia(request, slug):
    sloveniaexperiences = SloveniaExperience.objects.get(slugslovenia=slug)
    return render(request, 'blogslovenia.html', {'sloveniaexperiences': sloveniaexperiences})

def blogfrance(request, slug):
    franceexperiences = FranceExperience.objects.get(slugfrance=slug)
    return render(request, 'blogfrance.html', {'franceexperiences': franceexperiences})

我使用以下方式在HTML中称呼它们:

  

href =“ {%url'blogslovenia'slug = sloveniaexperience.slugslovenia%}”   href =“ {%url'blogfrance'slug = franceexperience.slugfrance%}”

也许有人可以帮助我...我已经进行了makemigrations和迁移,以便更新数据库,但是什么也没有。感谢您的阅读!

1 个答案:

答案 0 :(得分:0)

问题是,当您尝试从URL访问视图blogfrance时,您总是以blogslovenia结尾,因为它们都具有完全相同的url模式,而且我猜因为斯洛文尼亚是第一个列出的URL去斯洛文尼亚。因此,当您输入法国的子弹时,SloveniaExperience

正如@Daniel Roseman提到的,拥有这两个模型是没有意义的,只需将它们与一个额外的字段合并为一个,就像这样:

country_choices = (
    ("slovenia", "Slovenia"),
    ("france", "France"),
)

class FranceExperience(models.Model):
    author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
    exchange_country = CountryField(default='DEFAULT VALUE')
    category = models.IntegerField(choices=CATEGORY_CHOICES)
    title = models.CharField(max_length=200, default='DEFAULT VALUE')
    slug = models.SlugField(default='default-value')
    text = models.TextField(default='DEFAULT VALUE')
    created_date = models.DateTimeField(default=timezone.now)
    published_date = models.DateField(blank=True, null=True)
    country = models.CharField(max_length=30, choices=country_choices) # Added field

然后您可以拥有一个视图和一个URL。