Home > Net >  Django: URLs to apps showing 404 errors
Django: URLs to apps showing 404 errors

Time:05-11

Still needs working answer.

I have added three apps to my django website: application, blog, and feedback. All three have the same problem: when I click a link, or enter a URL, to any of them, I get a 404 error.

I'm attaching code and other documentation below for one of the problem addons. For further context, if necessary, my full code can be found at https://github.com/kkerwin1/pensdnd.

Directory structure

(venv) kris@adjutant:~/venv/pensdnd$ tree -if
.
./application
./application/admin.py
./application/apps.py
./application/forms.py
./application/__init__.py
./application/migrations
./application/models.py
./application/templates
./application/templates/application.html
./application/templates/application_thanks.html
./application/tests.py
./application/urls.py
./application/views.py
./blog
./blog/admin.py
./blog/apps.py
./blog/models.py
./blog/templates
./blog/templates/blog_list.html
./blog/templates/blog_post.html
./blog/tests.py
./blog/urls.py
./blog/views.py
./feedback
./feedback/admin.py
./feedback/apps.py
./feedback/forms.py
./feedback/models.py
./feedback/templates
./feedback/templates/feedback.html
./feedback/templates/feedback_thanks.html
./feedback/tests.py
./feedback/urls.py
./feedback/views.py
./manage.py
./pensdnd
./pensdnd/settings.py
./pensdnd/static
./pensdnd/static/css
./pensdnd/static/css/main.css
./pensdnd/static/html
./pensdnd/static/html/arvon_rules.html
./pensdnd/static/html/be_a_dm.html
./pensdnd/static/html/community_rules.html
./pensdnd/static/html/guild_rules.html
./pensdnd/static/html/index.html
./pensdnd/static/html/volunteer.html
./pensdnd/static/img
./pensdnd/static/img/carbon_fibre.png
./pensdnd/static/img/github_icon.png
./pensdnd/static/js
./pensdnd/static/misc
./pensdnd/static/templates
./pensdnd/static/templates/base.html
./pensdnd/static/templates/partials
./pensdnd/static/templates/partials/blogbar.html
./pensdnd/static/templates/partials/feedback.html
./pensdnd/static/templates/partials/footer.html
./pensdnd/static/templates/partials/navbar.html
./pensdnd/static/templates/partials/newsbar.html
./pensdnd/static/vid
./pensdnd/urls.py
./pensdnd/views.py
./pensdnd/wsgi.py
./requirements.txt

./pensdnd/urls.py

from django.contrib import admin
from django.urls import path, include
from . import views
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('', views.HomePageView.as_view()),
    path('admin/', admin.site.urls),
    path('be_a_dm/', views.BeADM.as_view()),
    path('blog/', include('blog.urls')),
    path('feedback/', include('feedback.urls')),
    path('application/', include('application.urls')),
    path('guild_rules/', views.GuildRules.as_view()),
    path('community_rules/', views.CommunityRules.as_view()),
    path('arvon_rules/', views.ArvonRules.as_view()),
    path('volunteer/', views.Volunteer.as_view()),
]   static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

./blog/urls.py

from . import views
from django.urls import path

urlpatterns = [
    path('blog/', views.PostList.as_view()),
    path('blog/<slug:slug>/', views.PostDetail.as_view(), name='post_detail'),
]

./blog/views.py

from django.shortcuts import render
from .models import Post
from django.views import generic

# Create your views here.

class PostList(generic.ListView):
    queryset = Post.objects.filter(status=1).order_by('-created_on')
    template_name = 'templates/blog_list.html'

class PostDetail(generic.DetailView):
    model = Post
    template_name = 'templates/blog_post.html'

./blog/models.py

from django.db import models
from django.contrib.auth.models import User

# Create your models here.

STATUS = (
    (0,"Draft"),
    (1,"Publish")
)

class Post(models.Model):
    title = models.CharField(max_length=200, unique=True)
    slug = models.SlugField(max_length=200, unique=True)
    author = models.ForeignKey(User, on_delete= models.CASCADE,related_name='blog_posts')
    updated_on = models.DateTimeField(auto_now= True)
    content = models.TextField()
    created_on = models.DateTimeField(auto_now_add=True)
    status = models.IntegerField(choices=STATUS, default=0)

    class Meta:
        ordering = ['-created_on']

    def __str__(self):
        return self.title

./blogs/templates/blog_list.html

{% extends 'static/templates/base.html' %}


<!doctype html>
<html lang="en">
<head>
    <title>
        {% block title %}
        PensiveDND :: Blog Posts
        {% endblock %}
    </title>
</head>

<body>
{% block pagecontent %}
    <section>

        {% for post in post_list %}
        <article>
            <h3>{{ post.title }}</h3>
            <p>{{ post.author }} | {{ post.created_on }}</p>
            <p>{{ post.content|slice:":200" }}
            <a href="{% url 'post_detail' post.slug  %}" >Read More</a>
        </article>

    </section>
{% endblock %}

</body>
</html>

./blogs/templates/blog_post.html

{% extends 'static/templates/base.html' %}


<!doctype html>
<html lang="en">
<head>
    <title>
        {% block title %}
        PensiveDND :: Blog :: {{ post.title }}
        {% endblock %}
    </title>
</head>

<body>
{% block pagecontent %}
    <section>

            <h2>{{ post.title }}</h2>
            <p>{{ post.author }} | {{ post.created_on }}</p>

            <p>{{ post.content | safe }}</p>

    </section>
{% endblock %}

</body>
</html>

CodePudding user response:

In "pensdnd/urls.py", you are missing the trailing slashes after your app name

for example

path('feedback', include('feedback.urls')),

should be

path('feedback/', include('feedback.urls')),

CodePudding user response:

Unable to access html files. There are no errors in your urls.py file. At least for the blog app. When I run the code, I don't get a 404 error with different edits. According to the Github codes, I changed the DIRS section in the templates field under settings.py to 'templates' and moved the html files you used in your blog application here. There was no problem. Editing all your code can be difficult, but that's where the problem lies.(Note that I only control the blog app.) also, you will get an error because you don't use {% endfor %}.

{% for post in post_list %}
    <article>
        <h3>{{ post.title }}</h3>
        <p>{{ post.author }} | {{ post.created_on }}</p>
        <p>{{ post.content|slice:":200" }}
        <a href="{% url 'post_detail' post.slug  %}" >Read More</a>
    </article>

If you have set templates location, you should just use template_name = 'blog_list.html instead of template_name = 'templates/blog_list.html'.

Please check these things again. As I said, the problem is caused by the use of static and templates. not urls.

  • Related