I can’t access a url, either fully or by Slug

Asked

Viewed 49 times

0

I’m taking a Django course that uses as a study a simplemooc platform, but I stopped in a class where when trying to access the course details that is in a separate url in a dynamic list, there is no change in the page, continues in the course listing without any change, the page only reloads. I tried to create through Course.objects.create, but nothing either. It doesn’t matter what you put after the / in 127.0.0.1:8000/courses/, whether whole, Slug or any other value, nothing changes.

The link of the first course in the comic is as: http://127.0.0.1:8000/admin/Courses/Course/13/change/

I’m guessing it’s something with the regular expression of the.py urls of Courses or some import missing. What problem?

py views.:

from django.shortcuts import render,get_object_or_404
from .models import Course
# Create your views here.
def index(request):
    courses = Course.objects.all()
    templateName='courses/index.html'
    context = {
        'courses': courses
    }
    return render(request,templateName, context)
def details(request,pk):
    course = Course.objects.get(pk=pk)
    templateName= 'courses/details.html'
    context = {
        'course':course
    }
    return render(request,templateName, context)

py urls.from Ourses:

from django.conf.urls import include,url

urlpatterns = [
    url(r'^$', 'index',name='index'),
    url(r'^(?P<pk>\d+)/$', 'details', name='details'),
]

simplemooc py.py urls:

from django.conf.urls import include,url
from django.contrib import admin
from simplemooc.core import views as coreViews
from simplemooc.courses import views as coursesViews
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^contato/', coreViews.contact, name="contact"),
    url(r'^cursos/', coursesViews.index, name="index"),
    url(r'^$', coreViews.home, name="home"),

]
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

inserir a descrição da imagem aqui

  • Try editing to this url, one you have similar to it in Ourses: url(r'(?P<pk> d+)/$', 'Details', name='Details'). No at the beginning..

1 answer

0

Have you tried using the path instead of the url? It’s much simpler to write, for example:

from django.urls import path
urlpatterns = [
    path('', views.random_view, name="random"),
]

You can also include your app as follows: In the folder of your app create the file "urls.py", on the page of your project use include so:

from django.urls import path, include
from django.contrib import admin

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('learn.urls'))
]
  • So, I’ve tried using the path, but it gives some errors like ImportError: cannot import name 'path'. This is because I am using version 1.11 of Django and not the new 2.0.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.