html forms are not showing in the template browsing - python

[solved]
i am trying to make a blog in django and on the github here is the code
i am trying set the html crispy form template in change password option and made some file names password_change_form.html, password_change_done.html etc. but when try to browse http://127.0.0.1:8000/accounts/password_change/done/ or any kind of pages related to password change section it is not showing the crispy form. but login or signup links are showing in crispy form. password change forms are showing in basic django form.
i want to change it into the desired ones.
i made two apps : blogapp and accounts. i am copying the urls below:
blogapp/urls.py:
from django.urls import path
from .views import (BlogappListView,
BlogappPostView,
BlogappCreateview,
BlogappUpdateView,
BlogappDeleteView,
)
urlpatterns = [
path('post/<int:pk>/delete/',BlogappDeleteView.as_view(),name='post_delete'),
path('post/<int:pk>/edit/',BlogappUpdateView.as_view(),name='post_edit'),
path('post/new/', BlogappCreateview.as_view(),name='post_new'),
path('post/<int:pk>/',BlogappPostView.as_view(),name='post_detail'),
path('',BlogappListView.as_view(),name='home'),
]
accounts/urls.py:
from django.urls import path
from .views import SignUpView
urlpatterns = [
path('signup/',SignUpView.as_view(),name='signup')
]
blog_project/urls.py:
from django.contrib import admin
from django.urls import path,include
from django.views.generic.base import TemplateView
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/',include('accounts.urls')),
path('accounts/',include('django.contrib.auth.urls')),
path ('',include('blogapp.urls')),
path('',TemplateView.as_view(template_name='home.html'),name='home')
]
i pur password change forms htmls files under templates/registration folder. here is onehtml file for example:
{% extends 'base.html' %}
{% block title %}Forgot Your Password?{% endblock title %}
{% load crispy_forms_tags %}
{% block content %}
<h1>Forgot your password?</h1>
<p>Enter your email address below, and we'll email instructions for setting
a new one.</p>
<form method="POST">
{% csrf_token %}
{{ form|crispy }}
<input class="btn btn-success" type="submit" value="Send me instructions!">
</form>
{% endblock content %}
this file is named as password_reset_form.html. there are are couple of othe files named password_change_done.html, password_reset_complete.html etc none of the password related files html are not showing ....all the urls are showing the basic django template.
i just can't figure out what am i missing and or what did i wrong? password change suppose to show the html form....not in django basic form.
please let me know where is my mistake.thanx in advance

I solved it. I changed templates directory from blogapp to project folder directory where manage.py exists. And everything else I did right. And it took a lot of time to figure out which is silly. Thanx any way... kept the answer if someone need this

Related

How to include in django template a reverse url in translation

I have the following block of code, And I want to include a url to the login page, after the user logs out, in a translatable text.
Unfortunately, translation blocks cannot include tags, and I get the following error:
SyntaxError: Translation blocks must not include other block tags: url "account:login"
{% blocktrans %}
You have been successfully logged out.
You can log-in again.
{% endblocktrans %}
urls.py:
from django.urls import path
from django.contrib.auth import views as auth_views
app_name = 'account'
urlpatterns = [
path('login/', auth_views.LoginView.as_view(), name='login'),
path('logout/', auth_views.LogoutView.as_view(), name='logout'),
]
What would be the right way to achieve something like this?
Edit: I figured there are workarounds, such as translating blocks of text separately, or using javascript to append the "href" element after the page loads. But I wonder if there is a more efficient, Django way.
As documented
Reverse URL lookups cannot be carried out within the blocktrans and
should be retrieved (and stored) beforehand:
{% url 'path.to.view' arg arg2 as the_url %}
{% blocktrans %}
This is a URL: {{ the_url }}
{% endblocktrans %}

Python + Django URL & View errors when attempting to POST to_do item

This is my first attempt at utilizing Django and I've run into an issue with the URL & Views files. I created a project called "reading". Within that project, I created two apps, "products" and "speech". Within the "speech" urls.py file, I have the following code:
from django.urls import path
from speech.views import todoView, addTodo
urlpatterns = [
path('todo/', todoView),
path('addTodo/', addTodo)
]
In the views.py file I have the following code:
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from .models import TodoItem
def todoView(request):
all_todo_items = TodoItem.objects.all()
return render(request, 'todo.html',
{'all_items': all_todo_items})
def addTodo(request):
new_item = TodoItem(content = request.POST['content'])
new_item.save()
return HttpResponseRedirect('/todo/')
In the project folder, within the urls.py file, I have:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('products/', include('products.urls')),
path('speech/', include('speech.urls'))
In the project folder, within the "templates" directory, I have a "todo.html" file with:
<h1>This is the todo page</h1>
<ul>
{% for todo_item in all_items %}
<li>{{ todo_item.content}}</li>
{% endfor %}
</ul>
<form action="/addTodo/" method="POST">{% csrf_token %}
<input type="text" name="content" />
<input type="submit" value="Add" />
</form>
When I attempt to add an item to the todo model, I get this error "The current path, addTodo/, didn't match any of these". I tried prepending "speech" to the form such as:
<form action="speech/addTodo/" method="POST">{% csrf_token %}
however, I now get this error:
The current path, speech/todo/speech/addTodo/, didn't match any of these.
Why is it duplicating the path when I prepend "speech" to the /addTodo action?
Not sure if this is associated to the error, but before I implemented a project level "templates" directory, I gave each app its own "templates" directory. Within those directories, I created an "index.html" file with separate content. When I had an index path for each apps, I couldn't get the apps to render the "index.html" file that was associated to it. Instead, it seemed the second application tried to render the "index.html" file of the first app. Is there a top down rule when it comes to templates in Django?
You missed a slash
Try this:
<form action="/speech/addTodo/" method="POST">{% csrf_token %}
A more 'Django' way of doing this could be:
from django.urls import path
from speech.views import todoView, addTodo
urlpatterns = [
path('todo/', todoView),
path('addTodo/', addTodo, name='add-todo')
]
Then in the template:
<form action="{% url 'add-todo' %}" method="POST">{% csrf_token %}

django can't show a template - how to fix

I just made the app users to validate user that is already registered in database. Included url inside project directory(urls.py), executed the login page in urls.py from app directory, made the template and a link refer in base.html. It all works, however when click Login link return this error:
TemplateDoesNotExist at users/login/
I tried to rename the path according tree navigation but always return this same error. Any idea what is happening?
Sorry my english
tree navigation in my project like this:
my_project
urls.py(project):
from django.contrib import admin
from django.urls import include, path
app_name = ['app_web_gym', 'users']
urlpatterns = [
path('admin/', admin.site.urls),
path('users/', include('users.urls', namespace='users')),
path('', include('app_web_gym.urls', namespace='app_web_gym')),
]
urls.py(app)
from django.urls import path
from django.contrib.auth import views as auth_views
from . import views
app_name = 'users'
urlpatterns= [
path('login/', auth_views.LoginView.as_view(template_name='users/login.html'), name='login'),
]
base.html:
<p>
Web Gym-
Clientes-
Treinos-
Instrutores-
{% if user.is_authenticated %}
<p>Hello, {{user.username}}.<p/>
{% else %}
Login
{% endif %}
</p>
{% block content %} {% endblock content %}
login.html:
{% extends 'app_web_gym/base.html' %}
{% block content %}
{% if form.errors %}
<p>Wrong username/password. Try again.</p>
{% endif %}
<form method='POST' action="{% url 'users:login' %}">
{% csrf_token %}
{{form.as_p}}
<button name='submit'>Log in</button>
<input type='hidden' name='next' value="{% url 'app_web_gym:index' %}" />
</form>
{% endblock content %}
I printed the full error:
TemplateDoesNotExist
I noticed that says Django tried loading these templates, in this order, in last line ->
/home/at_admin/prj01/app_web_gym/templates/users/login.html (Source does not exist)
thats the wrong path to login.html the correct is
/home/at_admin/prj01/users/templates/users/login.html as shown in tree navigation.
I don't know why is this happening and don't know how to fix it.
have you registered your app in main settings.py? It occurs sometimes when you forget to register your app
In main settings.py file:
at the end of, add one more line
INSTALLED_APPS = ['appname'] (replace appname with the name of your app having login page)
Hey people i just deleted all the app users and recreated it with the same coding that i previously copied. It worked this time though still don't know what happened.
Thanks all
First you should've checked if the app was installed in your settings. And as it looks in the error the files were being looked at the wrong place(app_web_gym/users/login.html) instead of (users/login.html). So I suppose you made a mistake in installing your app in settings.py.

cant see my blog post on my django website

im trying to follow this django tutorial, there a three section on my website called home blog and content.
what should happen, is i click on blog and it list out the blog post i made in the admin and i can click on the post and read what the blog post says, but when i click on blogs nothing shows up, its just blank.
i have already installed apps in settings
i added the url in the url.py folder
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('personal.urls')),
url(r'^blog/', include('blog.urls')),
I went into the models folder and created a model
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=140)
body = models.TextField()
date = models.DateTimeField()
def __str__(self):
return self.title
in my blogs/urls folder i have
from django.conf.urls import url, include
from django.views.generic import ListView, DetailView
from blog.models import Post
from django.urls import path
urlpatterns = [ path('', ListView.as_view(queryset=Post.objects.all().order_by("-date")[:25],
template_name="blog/blog.html")),
path('<int:pk>/', DetailView.as_view(model=Post,
template_name='blog/post.html'))]
in my blog folder i made a new directory called templates and in templates i made another directory called blog and in the folder i made an html file called blog.html and it is in a notepad++. this is what is in that blog.html notepad
{% extends "personal/header.html" %}
{% block content %}
{% for post in obejects_list %}
<h5>{{ post.date|date:"Y-m-d" }}<a href=
"/blog/{{post.id}}"> {{post.title}}</a></h5>
{% endfor %}
{% endblock %}
and i went into admin folder and added
from django.contrib import admin
from blog.models import Post
admin.site.register(Post)
try to add context_object_name to your ListView class like this :
path('', ListView.as_view(queryset=Post.objects.all().order_by("-date")[:25],
template_name="blog/blog.html", context_object_name = "posts")),
then in your template do like this :
{% extends "personal/header.html" %}
{% block content %}
{% for post in posts %}
<h5>{{ post.date|date:"Y-m-d" }}<a href=
"/blog/{{post.id}}"> {{post.title}}</a></h5>
{% endfor %}
{% endblock %}

how to work with two apps in django

Hello im learning django and i would like to know if its a way to make my two apps work i have my folders like this
mysite/
--/app1 (blog app)
--/app2 (a list of users that signs up with a form)
--/mysite
--db
--manage.py
my app2 has the index.html and the structure.html which i use to inheritate my others html files, so
im trying to use the post_list.html file that i made in my app1, first of all this is how my urls looks something like this
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'info', 'app2.views.grid'),
url(r'', 'app2.views.home'),
url(r'^blog/', 'app1.views.post_list')
)
my app1 views looks like this
from django.shortcuts import render
from .models import Post
# Create your views here.
def post_list(request):
posts = Post.objects.filter(published_date_isnull=False).order_by('published_date')
return render(request, 'app1/post_list.html',{'posts':posts})
then my post_list.html looks like this
{% extends "/app2/templates/app2/structure.html" %}
{% block content %}
{% for post in posts %}
<div class="post">
<div class="date">
{{ post.published_date }}
</div>
<h1>{{ post.title }}</h1>
<p>{{ post.text|linebreaks }}</p>
</div>
{% endfor %}
{% endblock %}
when i enter to my browser and type 127.0.0.1:8000/blog/ it keeps apearing my app2/index.html, what am i missing? do i have to do anything else than adding my views to my url like i did?
aditional info: i added my app2 and my app1 to my settings thoe
I think your urls.py needs to be updated:
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^info', 'app2.views.grid'),
url(r'^$', 'app2.views.home'),
url(r'^blog/', 'app1.views.post_list')
)
url(r'', 'app2.views.home') will act like a wild card since python uses regular expressions to match. Take a look at the URL dispatcher documentation for a better understanding of what's going on.

Categories

Resources