I just started with a Django project using django-allauth, I configured the basic settings, without using any 3rd party provider. I have set up the urls.py of my project and urls.py of my app.
But on going to http://localhost:8000, I am getting to 'home.html' but how do I remove the navigation of allauth
The following is the urls.py of my project :
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('allauth.urls')),
path('ckeditor/',include('ckeditor_uploader.urls')),
path('',include('blog.urls')),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
And this my urls.py of app :
from django.urls import path, include
from . import views
urlpatterns = [
path("", views.PostListView.as_view(), name = 'post_list'),
path("post/add", views.CreatePostView.as_view(), name = "create_new_post"),
]
views.py
from django.shortcuts import render
from django.views.generic import ListView, View
# Create your views here.
from .forms import PostForm, CommentForm
from .models import Post, Comment
class PostListView(ListView):
queryset = Post.objects.filter(is_published=True)
template_name = 'home.html'
class CreatePostView(View):
form_class = PostForm()
template_name = 'create_post.html'
model = Post
home.html
{% extends 'base.html' %}
{% block content %}
<h1>Hello World</h1>
{% for post in post_list %}
<h1>{{post.post_title}}</h1>
<p>{{post.post_body|safe}}</p>
{% endfor %}
{% endblock %}
path("post/add/", views.CreatePostView.as_view(), name = "create_new_post"),
add trailing slash to your url
your global urls.py:
path('',include('blog.urls')),
add something in your app urls.py:
path('test/',views.PostListView.as_view()),
after adding this to your urls.py, run your app again
the extended base.html file may contain the navigation. make changes there to remove or simply remove it
{% extends 'base.html' %}
Related
To elaborate, I went down a bit of a rabbit hole just trying to make a trailing "/" optional in the URL. got it to sorta work with the project directory, but when on the blog app, I tried to use re_path in order to use regex to allow for an optional trailing "/" in the url, but I seem to get an error in the template
website urls.py (directory with settings.py)
from django.contrib import admin
from django.urls import path, include, re_path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('main.urls')),
path('blog/', include('blog.urls')),
path('blog', include('blog.urls'))
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
blog urls.py
from django.urls import path, include, re_path
from .views import BlogView, ArticleView
urlpatterns = [
path('', BlogView.as_view(), name="blog"),
re_path(r'titles/(?P<slug_url>\d+)/?$', ArticleView, name="article")
]
blog.html template error appears at the href
{% extends 'base.html' %}
{% block content %}
<h1>Articles</h1>
{% for post in object_list %}
<h2>{{ post.title }}</h2>
{% endfor %}
{% endblock %}
blog views.py
from django.shortcuts import render, get_object_or_404, redirect
from .models import Post
from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView
from django.urls import reverse_lazy, reverse
from django.http import HttpResponseRedirect
# Create your views here.
class BlogView(ListView):
model = Post
template_name = 'blog.html'
def ArticleView(request, url_slug):
post = get_object_or_404(Post, url_slug = url_slug)
return render(request, 'article.html', {'post':post})
Any help would be greatly appreciated, I just find the 404 I get without a trailing / to be very annoying
I have a ran into a difficulty when navigating between different templates in a Django (v3.2) application. The app is called 'manage_remittance'.
The default landing page (which uses template manage_remittance/templates/manage_remittance/view_remittance.html) for the app should show a list of items (list is not relevant at the moment), and at the top of that list there should be a link, leading to another page in the same app, which would allow to add new items to the list.
The form that is invoked first is here:
manage_remittance/templates/manage_remittance/view_remittance.html
{% extends "root.html" %}
{% load static %}
{% load crispy_forms_tags %}
{% url 'manage_remittance:remittance_add' as remittance_add %}
{% block title %}
VIEW REMITTANCES PAGE
{% endblock title %}
{% block content %}
<div class="list-group col-6">
Click here to add remittance data
</div>
I want to be able to get to another template (manage_remittance/templates/manage_remittance/remittance_add.html), but the {{ remittance_add }} has no value.
In addition, when I specify exact name of the html file (remittance_add.html) in the a href (see above), and click on it, I get a following error:
Using the URLconf defined in sanctions_project.urls, Django tried these URL patterns, in this order:
admin/
[name='login']
login/ [name='login']
logout/ [name='logout']
manage_remittance/ [name='view_remittance']
manage_remittance/ remittance_add/ [name='create_remittance']
^static/(?P<path>.*)$
^media/(?P<path>.*)$
The current path, manage_remittance/remittance_add.html, didn’t match any of these.
What am I doing wrong here?
fragment of urls.py for the project:
urlpatterns = [
path('admin/', admin.site.urls),
path('', login_view, name='login'),
path('login/', login_view, name='login'),
path('logout/', logout_view, name='logout'),
path('manage_remittance/', include('manage_remittance.urls')), # namespace='manage_remittance'
]
urls.py at manage_remittance app:
from .views import (
CreateRemittanceInfo,
RemittanceListView
)
app_name = 'manage_remittance'
urlpatterns = [
path('', RemittanceListView.as_view(), name='view_remittance'),
path('remittance_add/', CreateRemittanceInfo.as_view(), name='create_remittance'),
]
views.py at manage_remittance app:
from django.shortcuts import render, get_object_or_404
from django.urls import reverse
from django.views.generic import ListView
from django.views.generic.edit import CreateView
from django.contrib.auth.mixins import LoginRequiredMixin
from .models import Remittance
class CreateRemittanceInfo(LoginRequiredMixin, CreateView):
model = Remittance
fields = ['remittance_text']
template_name_suffix = '_add'
class RemittanceListView(ListView):
model = Remittance
template_name = 'manage_remittance/view_remittance.html'
It seems that you have an issue in your urls:
manage_remittance/ remittance_add/ [name='create_remittance']
the name does not match with
manage_remittance:remittance_add
It seems quite simple: in your template, you should display a list, which is not the case. Have a look at Django User Guide: https://docs.djangoproject.com/fr/3.2/ref/class-based-views/generic-display/
It should look like
{% for object in object_list %}
<p>{{object.remittance_text}}</p>
{% endfor %}
I am trying to link two pages. Page files are home.html and pageOne.html. I am getting "NoReverseMatch at /'blog' is not a registered namespace". I am using django. When I first created the app, I named it artclBlog, I then created a templates folder and another folder within that one, this one I named blog. I think I should have kept these two names the same, this may have caused some confusion in my code.
pic of error
my views.py
from django.shortcuts import render, get_object_or_404
from .models import Blog
def home(request):
blogs = Blog.objects.order_by('-date')
return render(request, 'blog/home.html', {'blogs': blogs})
my urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings
from artclBlog import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='home'),
path('<int:blog_id>/', views.PageOne, name='PageOne')
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
my models.py
from django.db import models
class Blog(models.Model):
title = models.CharField(max_length=200, default='')
summary = models.CharField(max_length=200, default='')
pageOne = models.TextField(default='')
pageTwo = models.TextField(default='')
pageThree = models.TextField(default='')
pageFour = models.TextField(default='')
date = models.DateField(default='')
def __str__(self):
return self.title
home.html
{% for blog in blogs %}
<a href="{% url 'blog:PageOne' blog.id %}">
<h2 id="txt">{{ blog.title }}</h2>
</a>
<h4 id="txt">{{ blog.date|date:'M d y' }}</h4>
<p id="txt">{{ blog.summary|truncatechars:190 }}</p>
<hr>
{% endfor %}
You did not define an app_name in your urls.py, hence that means that blog: in blog:PageOne makes no sense. You thus either define an app_name, or remove the namespace.
Option 1: Add an app_name in urls.py
You can specify the namespace by writin an app_name in the urls.py:
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings
from artclBlog import views
app_name = 'blog'
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='home'),
path('<int:blog_id>/', views.PageOne, name='PageOne')
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This however means that thus all views now use this namespace, including home for example.
Option 2: Remove the namespace
Another option is to remove the namespace in the {% url … %} template tag [Django-doc]:
{% for blog in blogs %}
<a href="{% url 'PageOne' blog.id %}">
<h2 id="txt">{{ blog.title }}</h2>
</a>
<h4 id="txt">{{ blog.date|date:'M d y' }}</h4>
<p id="txt">{{ blog.summary|truncatechars:190 }}</p>
<hr>
{% endfor %}
This mistake comes usually when you have created different apps in your project.
First you do:
django-admin startproject mainapp
Then you do
python manage.py startapp secondapp
urls.py in your secondapp folder would automatically have app_name = 'secondapp'. However you must add in mainapp urls.py the following:
urlpatterns = [
...
path('secondapp/',include('secondapp.urls',namespace='secondapp')),
...
]
This is because your mainapp is the one that has the main access to the project and needs to know your secondapp urls.
So I have been trying to access a project post that I have created for my website, but every time I click on it the url can't be found. I am not sure why.
My code is below:
mywebsite/urls.py
from django.contrib import admin
from django.urls import re_path, include
from django.conf import settings
from django.conf.urls.static import static
#re_path(r'^admin/', admin.site.urls),
urlpatterns = [
re_path(r'^admin/', admin.site.urls),
re_path(r'^', include('home.urls')),
re_path(r'^projects/', include('projects.urls', namespace="create_post")),
re_path(r'^contact/', include('contact.urls')),
]
projects/urls.py
from django.urls import re_path, include
from . import views
# urls for projects page
app_name = 'create_post'
urlpatterns = [
re_path(r'^$', views.retrieve_projects, name="retrieve_projects"),
#re_path(r'^create/$', views.CreateProjectsView.as_view(), name="create_projects"),
re_path(r'^create/$', views.CreateProjectsView.as_view(), name="create_projects"),
re_path(r'^(?P<slug>[\w-]+)/$', views.details_of_project, name="details_of_project"),
re_path(r'^(?P<slug>[\w-]+)/update/$', views.update_projects, name="update_projects"),
re_path(r'^(?P<slug>[\w-]+)/delete/$', views.delete_projects, name="delete_projects"),
]
# To make images work
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
projects/views.py
# Function to retrieve all different projects
def retrieve_projects(request):
# Retrieves objects based on latest publish date
projects_list = Projects.objects.all().order_by("-publish_date")
context = {
'projects_list': projects_list,
}
return render(request, 'projects/projects.html', context)
projects/projects.html
<h1>asdasdasdasdasdasdas</h1>
{% if projects_list %}
<ul>
{% for project in projects_list %}
<h2>{{project.title}}</h2>
<h2>{{project.description}}</h2>
<h2>{{ project.publish_date }}</h2>
{% endfor %}
</ul>
{% endif %}
The error returns page not found
try this
within an app I have two models, named Course and Step. Every Step belongs to a Course and each Course has many steps. However, I'm having problem creating a detailview for Steps. For example when i go to the url 'http://127.0.0.1:8000/courses/1' it should display steps for course.objects.get(pk=1). However what i get back is just the page for course, i.e, http://127.0.0.1:8000/courses'.
Model
from django.db import models
# Create your models here.
class Course(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
title = models.CharField(max_length= 255)
description = models.TextField()
def __str__(self):
return self.title
class Step(models.Model):
title = models.CharField(max_length = 255)
description = models.TextField()
order = models.IntegerField()
course = models.ForeignKey(Course)
def __str__(self):
return self.title
Url
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.course_list),
url(r'(?P<pk>\d+)/$', views.course_detail)
]
View
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
from .models import Course, Step
def course_list(request):
courses = Course.objects.all()
return render(request, 'courses/course_list.html', {'courses': courses})
def course_detail(request, pk):
course = Course.objects.get(pk=pk)
return render(request, 'courses/course_detail.html', {'course': course})
course_detail.html
{% extends 'layout.html' %}
{% block title %}{{course.title}}{% endblock %}
{% block content %}
<article>
<h2>{{ course.title }} %</h2>
{{course.description}}
<section>
{% for step in course.step_set.all %}
<h3>{{ step.title }}</h3>
{{step.description}}
{% endfor %}
</section>
</article>
{% endblock %}
Main Urls
from django.conf.urls import url, include
from django.contrib import admin
from courses.views import course_list
from django.conf import settings
from django.conf.urls.static import static
from . import views
urlpatterns = [
url(r'^courses/', course_list),
url(r'^admin/', admin.site.urls),
url(r'^$', views.hello_world),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
I just can't seem to recognize where i went wrong
I think the you need these url mappings..! This should do the trick.
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^applicationName/$', views.ListView.as_view(), name='courses'),
url(r'^applicationName/(?P<pk>\d+)$', views.CourseDetailView.as_view(), name='course-detail'),
]
The problem is in your main urls.py file - this line makes any URL path starting with 'courses/' resolve to the course_list view:
url(r'^courses/', course_list),
So Django will never reach the course_detail view. Instead, you must "include" the application-specific URLs:
url(r'^courses/', include('myproject.myapp.urls'),
Where myproject is your main project name, and myapp is your app name (make sure you also registered your application under INSTALLED_APPS at your settings.py file).