Omega
Omega

Reputation: 267

django issue with {% url '..' %}

i don't know exactly how to explain this, but i will try...Something weird is going on with the linking of a tag...Basically when i link a <h1> tag or anything to go to another template tag, it doesn't work...it does change the url extension but it stays on the same template...

I will show you the files now... This is the tree of the folder project:

tube/
├── main
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   │   ├── __init__.py
│   ├── models.py
│   ├── tests.py
│   ├── urls.py
│   ├── views.py
├── manage.py
├── models.py
├── templates
│   ├── base.html
│   └── main
│       ├── details.html
│       └── main.html
└── tube
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    ├── views.py
    ├── wsgi.py

main/views.py :

class Main(TemplateView):
    template_name = 'main/main.html'

    def get_context_data(self, **kwargs):
        context = super(Main, self).get_context_data(**kwargs)
        test = Test.objects.all().order_by('ps_name')
        # for i in range(200):
        #     lines.append('Line %s' % (i + 1))
        paginator = Paginator(test, 20)
        page = self.request.GET.get('page')
        try:
            show_lines = paginator.page(page)
        except PageNotAnInteger:
            # If page is not an integer, deliver first page.
            show_lines = paginator.page(1)
        except EmptyPage:
            # If page is out of range (e.g. 9999), deliver last page of results.
            show_lines = paginator.page(paginator.num_pages)
        context['test'] = show_lines
        return context


def details(request, pk):
    ps = Test.objects.get(id=pk)
    print ps
    return render(request, 'main/details.html', {'test': ps})

As you can see my first view goes to main.html and my second to details.html

main/urls.py :

urlpatterns = [
    url(r'$', views.Main.as_view(), name='main'),
    url(r'(?P<pk>\d+)/', views.details, name='details'),
]

this is my templates/main/main.html :

{% extends 'base.html' %}
{% load staticfiles %}
{% load static %}
{% load bootstrap4 %}
{% block content %}
    <div class="container">
        <table class="table table-bordered">
            <thead>
            <tr>
                <th>Name</th>
                <th>Rank</th>
            </tr>
            </thead>
            <tbody>
            {% for ps in test %}
                <tr>
                    <td>
                        <a href="{% url 'main:details'  pk=ps.id %}">{{ ps.ps_name }}</a>
                    </td>
                    <td>{{ ps.ps_rank }}</td>
                </tr>
            {% endfor %}
            </tbody>
        </table>

        {% bootstrap_pagination test url="/test?page=1" size="small" %}
    </div>
{% endblock %}

and this is just a test part of templates/main/details.html :

<div class="container">
        <table class="table table-bordered">
            <thead>
            <tr>
                <th>Bio</th>
            </tr>
            </thead>
            <tbody>
            {% for ps in test %}
                <tr>
                    <td>{{ ps.bio }}</td>
                </tr>
            {% endfor %}
            </tbody>
        </table>
    </div>

So my question is...when i click any link from main/main.html it doesn't show me the content of main/details.html it shows me the same content from main/main.html but it does change the url extention. Does anyone know why? Cause on other projects i don't have a problem like this...it works fine.

Thank you

Versions:

Django==1.11.10
django-bootstrap4==0.0.6

UPDATE

Changing url extension: main/main.html is on https://example.com/main ....and main/details.html is on https://example.com/main/(pk.id) ....basically when i click a name on https://example.com/main it goes to https://example.com/main/101...but the page doesn't change

Upvotes: 0

Views: 47

Answers (2)

Astik Anand
Astik Anand

Reputation: 13047

Your urls should be like below, you are missing ^

urlpatterns = [
    url(r'^$', views.Main.as_view(), name='main'),
    url(r'^(?P<pk>\d+)/$', views.details, name='details'),
]

Upvotes: 1

Daniel Roseman
Daniel Roseman

Reputation: 599758

You're missing the anchor from the main URL; currently it just means "any string that ends", which of course matches everything. It should be:

url(r'^$', ...

Upvotes: 2

Related Questions