Studentofcode
Studentofcode

Reputation: 45

No reverse match django with slug_pattern

So I want to insert a url from urls.py, but because we have a slug_pattern arg, i'm confuse how to do that.

Below are the traceback :

Environment:


Request Method: GET
Request URL: http://localhost:8000/universitas-indonesia

Django Version: 1.11.20
Python Version: 2.7.15
Installed Applications:
('neliti',
 'publisher',
 'accounts',
 'hal',
 'mtam',
 'haystack',
 'django_countries',
 'storages',
 'select2',
 'modeltranslation',
 'tastypie',
 'mathfilters',
 'fastsitemaps',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.sites',
 'django.contrib.redirects',
 'debug_toolbar',
 'django_extensions')
Installed Middleware:
('corsheaders.middleware.CorsMiddleware',
 'django.middleware.locale.LocaleMiddleware',
 'publisher.middleware.CustomDomainMiddleware',
 'publisher.middleware.IndexingMiddleware',
 'django.middleware.gzip.GZipMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.middleware.common.CommonMiddleware',
 'publisher.middleware.RemoveSlashMiddleware',
 'publisher.middleware.ToLowercaseMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.common.BrokenLinkEmailsMiddleware',
 'django.contrib.redirects.middleware.RedirectFallbackMiddleware',
 'debug_toolbar.middleware.DebugToolbarMiddleware',
 'publisher.middleware.NonHtmlDebugToolbarMiddleware')


Template error:
In template E:\Project\neliti\backend\publisher\templates\publisher\headers\sub-menu\sub_menu_repository.html, error at line 51
   Could not parse the remainder: '-indonesia' from 'universitas-indonesia'   41 :       <li>
   42 :         <a href="#">Policies</a>
   43 :       </li>
   44 :       <li>
   45 :         <a href="#">For developers</a>
   46 :       </li>
   47 :     </ul>
   48 :   </div>
   49 : 
   50 :   <div class="section">
   51 :     <a id="contact" href=" {% url 'organisation_detail' slug=universitas-indonesia %} ">
   52 :       <div class="header">
   53 :         CONTACT
   54 :       </div>
   55 :     </a>
   56 :   </div>
   57 : </div>

The urls.py look like this

we use OrganisationDetailView and slug_pattern

slug_pattern = '(?P<slug>[a-z0-9_\\-]+)'

url(r'^%s$' % slug_pattern, cache_page(content_page_cache_time)(OrganisationDetailView.as_view()), name='organisation_detail'),
    prefix_default_language=False
)

My Organisation view look like this

class OrganisationDetailView(NelitiDetailView):
    model = Organisation
    filter_set = 'Publication'
    template_name = 'publisher/organisation_detail.html'
    display_filters = ['language', 'year', 'contribution', 'type', 'grade', 'country', 'province']

    def get_select_and_prefetch_objects(self):
        return self.model.objects.select_related(
            'primary_type', 'country', 'style_settings'
        ).prefetch_related(
            'parents', 'disciplines'
        )

    def get_search_box_placeholder(self):
        return _("Search %(name)s repository") % {'name': self.object.name_short}

    def get_narrow_query(self):
        # return 'author_org:"%(pk)s" OR funder:"%(pk)s" OR publisher:"%(pk)s"' % {'pk': self.object.pk}
        return 'repository:"%s"' % self.object.pk

    def get_context_data(self, **kwargs):
        # Add journal_lists and conference_lists to context

        # order_by('title') doesn't work, probably because half the titles are in title_en and half in title_ind
        journal_set = sorted(Journal.objects.indexed().filter(
            Q(primary_publisher__in=self.object.descendants) | Q(secondary_publishers__in=self.object.descendants)).distinct()
            , key=lambda x: x.title)
        # According to Anton, don't include proceedings published by descendent organisations
        conference_set = sorted(ConferenceNew.objects.indexed().filter(
            Q(primary_organiser=self.object) | Q(secondary_organisers=self.object)).distinct()
            , key=lambda x: x.name, reverse=True)

        for obj_set, key in [(journal_set, 'journal_lists'), (conference_set, 'conference_lists')]:
            n = len(obj_set)
            if n == 0:
                kwargs[key] = []
            elif n <= 6:
                kwargs[key] = [obj_set]
            else:
                split_point = int(math.ceil(n/2))
                kwargs[key] = [
                    obj_set[:split_point],
                    obj_set[split_point:],
                ]
        favicon = get_favicon_href(self.model, self.object)
        kwargs['favicon'] = favicon
        return kwargs

I've tried to do any related combination to insert the url, but found no solution, if you can give any insight it'll be helpful

Thanks for any help

Upvotes: 0

Views: 92

Answers (1)

Akash Joshi
Akash Joshi

Reputation: 106

Update your html <a> tag. universitas-indonesia should also be in single quotes slug='universitas-indonesia'

<div class="section">
 <a id="contact" href=" {% url 'organisation_detail' slug='universitas-indonesia' %} ">
    <div class="header">
       CONTACT
     </div>
 </a>
</div>

Get slug dynamically assuming URL has ?var1='universitas-indonesia'

<div class="section">
 <a id="contact" href=" {% url 'organisation_detail' slug=request.GET.var1 %} ">
    <div class="header">
       CONTACT
     </div>
 </a>

Do update your var1 variable according to your URL.

Upvotes: 1

Related Questions