How to display url as text and not number from DB?

I'm new to Python, I have a problem with how the url is displayed in the address bar. At this point the url is localhost: 8000/1 / I would like the url to be localhost: 8000 / entry name in the / box Can someone please give me a hint.

Project / urls.py

from django.conf.urls import include,  url 
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import admin

urlpatterns = [
 url(r'^$', 'papers.views.index', name='index'),
 url(r'^(?P<question_id>[0-9]+)/$', 'papers.views.detail', name='detail'),
 url(r'^admin/', include(admin.site.urls)), 
 ]   

urlpatterns += staticfiles_urlpatterns()

      

docs / views.py

from django.shortcuts import render
from .models import Question


def index(request):
   latest_question_list = Question.objects.all
   context = {'latest_question_list': latest_question_list}
   return render(request, 'papers/index.html', context)

def detail(request, question_id):
    question = Question.objects.get(pk=question_id)
    return render(request, 'papers/detail.html', {'question': question})

      

docs / templates / docs / detail.html

{% extends "master2.html"  %}
{% block h1 %}  

<div class="center">
    <h4>{{question.naslov}} </h4>
    <p>{{question.opsirnije_text}}</p>
</div>

{% endblock %}
{% block title  %} Detail {% endblock %}

      

docs / models.py

from django.db import models

class Question(models.Model):
  naslov = models.CharField(max_length=200)
  opis = models.CharField(max_length=200)
  datum_objave = models.DateTimeField('date published')
  opsirnije_text = models.TextField(max_length=20000)

def __str__(self):             
    return self.naslov

def was_published_recently(self):
    return self.datum_objave >= timezone.now() -  datetime.timedelta(days=1)

      

+3


source to share


2 answers


Foreword: First of all, make it clear in which direction the data is displayed. You don't assign URLs to objects in your database; requests can be for any url and then Django has to figure out what that means and how to respond.
ie it {url} -> looks up object

, not{object} -> has a URL

Here's your url:

url(r'^(?P<question_id>[0-9]+)/$', 'papers.views.detail', name='detail'),

      

And here's where you handle it:

def detail(request, question_id):
    question = Question.objects.get(pk=question_id)

      

So what happens when someone types in localhost:8000/34/

, for example, it matches the "base url followed by numbers" regex rule. If you want to change this to "base url followed by name" you can do this:



url(r'^(?P<question_name>[\w_-]+)/$', 'papers.views.detail', name='detail'),

      

(Specifically, there are letters or a hyphen or underscore here, change the regex to match what your "question name" looks like.)

Then you will need to deal with the fact that your handler has to accept names:

def detail(request, question_name):
    question = Question.objects.get(naslov=question_name)

      

The limitation here is that the query .get

must produce a unique result; if possible, if multiple questions have the same name, you are in trouble.

+2


source


You need to first change the URL pattern papers.view.detail

to not match integers - something like \w+

instead [0-9]+

. You probably want to rename the capture group. See https://docs.python.org/2/library/re.html .



Then you need to change the method detail

to accept an argument that matches the capturing group name and query for an object on that argument (which I think is a field naslov

?).

+1


source







All Articles