How to switch HTML page in Python with Django - python

Template :
<a class="btn btn-primary" href="{% url 'edit' %}">Edit</a>รน
views.py:
def edit(request):
return render(request, "edit.html")
urls.py:
urlpatterns = [
path("", views.index, name="index"),
path("wiki/create", views.create, name="create"),
path("wiki/edit", views.edit, name="edit"),
path("wiki/<str:name>", views.entry, name="entry"),
path("search", views.search, name="search"),
path("save", views.save, name="save"),
path("random", views.random, name="random"),
]
I would like simply to switch from an HTML page on another, but the function gives me this error:
TemplateDoesNotExist at /wiki/edit
edit.html
But the template exist, I created it. I tryed a lot of changes but all of them gives me error. Thank you.

The Application name needs to be referenced in the template name.
Like so:
return render(request, "encyclopedia/edit.html")

Related

Why is the html not recognising the view function?

I am trying to build an inventory management with Django.
When building delete option, I am encountering an error.
The error message is "Reverse for 'delete' not found. 'delete' is not a valid view function or pattern name."
I will paste my code below.
Delete function code:
def delete(request, iid):
obj = inventory.objects.get(id=iid)
obj.delete()
return render(request, 'main/lists.html')
HTML Template code:
<a class="btn btn-danger bg-gradient" href="{% url 'delete' i.id %}" role="button">Delete</a>
Urls.py code:
urlpatterns = [
path('', views.index, name="index"),
path('lists', views.lists, name="lists"),
path('add', views.add, name="add"),
path('edit', views.edit, name="edit"),
path('delete/<int:id>', views.delete, name="delet") ]
Please give me a valid solution.
In urls.py the url pointing to the method is called delet.
Update it to delete on urls.py or to delet on the HTML template.
From the docs you can do this:
{% url 'delet' i.id as the_url %}
<a class="btn btn-danger bg-gradient" href="{{ the_url }}" role="button">Delete</a>

Delete post in django

I know similar kind of question is asked before, but I was not able to get it. This is my first project as a Django beginner.
In my Django blog app, I made a delete button but it is not working and I am finding for answers, trying different methods on the web but it did not help.
I am trying to do is when admin open the post, then on clicking the delete button, it take the post-id and delete that post and redirect to home page, but it is not working as expected. So, lastly I came here. Any help will be appreciated. Thanks!
This is my urls.py file:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('post/<int:pk>', views.post, name='post'),
path('about', views.about, name='about'),
path('contact_us', views.contact_us, name='contact_us'),
path('register', views.register, name='register'),
path('login', views.login, name='login'),
path('logout', views.logout, name='logout'),
path('create_post', views.create_post, name='create_post'),
path('delete_post', views.delete_post, name='delete_post')
]
This is my views.py file:
def delete_post(request, *args, **kwargs):
pk = kwargs.get('pk')
post = get_object_or_404(Post, pk=pk)
if request.method == 'POST':
post.delete()
return redirect('/')
return render(request, 'delete-post.html')
This is delete post html form:
<form action="{% url 'delete_post' post.id %}" method="post">
{% csrf_token %}
<input type="submit" value="Delete post">
</form>
Delete button:
<button type="button" class="btn btn-danger" style="position:relative; right: -1145px;">Delete</button>
for deleting a post
def delete_post(request, id):
post = Post.objects.filter(id=id)
address.delete()
return redirect('/')
and in your html
<a class="btn btn-outline-danger" href="{% url 'appname:delete_post' id=post.id %}">Delete It</a>
and in your urls.py
path('<int:id>/delete-post',views.delete_post,name='delete_post')

Reverse for 'display_data' not found. 'display_data' is not a valid view function or pattern name

I am getting the below error when I am trying to load the home page:
Reverse for 'display_data' not found. 'display_data' is not a valid view function or pattern name
My views.py file is as follows:
def home(request):
#query_results = QRC_DB.objects.all()
return render(request, 'display_data.html')
def display_data(request,component):
#query_results = QRC_DB.objects.all()
return HttpResponse("You're looking at the component %s." % component)
My urls.py file under the app is as follows:
from django.urls import path
from fusioncharts import views
urlpatterns = [
path('home/', views.home, name=''),
]
The urls.py file under the project is as follows:
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('fusioncharts.urls'))
]
And my html file (display_data) code is as follows :
{% block content %}
<h3>Display the test results</h3>
<div id="container" style="width: 75%;">
<canvas id="display-data"></canvas>
<li>SQL</li>
</div>
{% endblock %}
Can anyone please help me to find out the mistake ?
Thanks.
Your urls.py file doesn't contain any url for display_data.
When you're trying to click the link rendered in the HTML tag, namely,
<li>SQL</li>
it tries to resolve the URL display_data.
First, it checks the root urls.py file. Among the following:
path('admin/', admin.site.urls),
path('', include('fusioncharts.urls'))
it matches the second one. Then it loads the fusioncharts.urls but the fusioncharts.urls doesn't contain any URL for display_data. That's why you are getting the error.
The urls.py file should be like this:
from django.urls import path
from fusioncharts import views
urlpatterns = [
path('home/', views.home, name=''),
path('display_data/<str:arg>', views.display_data, name='display_data'),
]
# There is a change in urls.py and in your template 'display_data.html'
urls.py
urlpatterns = [
path('home/', views.home, name=''),
path('display_data/<str:component>', views.display_data, name='display_data'),
]
display_data.html
{% block content %}
<h3>Display the test results</h3>
<div id="container" style="width: 75%;">
<canvas id="display-data"></canvas>
<li>SQL</li>
</div>
{% endblock %}

No Reverse Match with Django Auth Views

I created a custom User model following the example in the Django documentation, now I'm trying to use Django auth views but I keep getting NoReverseMatch at /accounts/login/
Reverse for 'django.contrib.auth.views.login' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
This is the url conf:
from django.contrib.auth import views as auth_views
urlpatterns = [
url(r'^accounts/login/$', auth_views.login, {'template_name': 'login.html',
'authentication_form': LoginForm}),
url(r'^logout/$', auth_views.logout, {'next_page': '/accounts/login'}),
url(r'^$', home, name="home"),
]
And I have this line in my template:
<form method="post" action="{% url 'django.contrib.auth.views.login' %}">
{% url 'django.contrib.auth.views.login' %}
This is incorrect. We put the name given to the url here instead of the location of the view.
Please see https://docs.djangoproject.com/en/1.10/ref/templates/builtins/#url. You need to provide a name for login like you have for home and then use that.
Correct way is:
urls.py ->
url(r'^accounts/login/$', auth_views.login, {'template_name': 'login.html','authentication_form': LoginForm}, name="login") ,
template ->
<form method="post" action="{% url 'login' %}">

Django how to save model in view

I'm new to Django. I'm having an issue where I can't save my model in the views.py. The concept is to have an input field where a user can type in a name, then using request.POST.get('attribute_name') I can save my model, but it's not working. When I print a list of all the objects in that model there's nothing there, even though I don't get an error message during all of this.
template:
<form id="save_form" method="post" action="{% url 'project_view.views.projectz_save' %}">
{% csrf_token %}
<table>
<tr>
<td>Project Name</td>
<td><input name="projectz_name"/></td>
</tr>
</table>
<input type="submit" value="Save" />
</form>
views.py:
def projectz_save(request):
try:
p = Project(name=request.POST.get('projectz_name'))
p.save()
return redirect('http://www.google.com/')
except:
return redirect('http://www.google.com/')
app urls:
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^$', views.register, name='register'),
url(r'^$', views.projectz_save, name='project_save'),
)
site urls:
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^project_view/', include('project_view.urls')),
I even put in some silly redirect code to google.com just to see if the views.py was even executing, but it's not working, though like I said there are no error messages, the page just refreshes. I'm sure I'm doing wrong that's easy to fix, but I'm a noobie. :D
Ok I think maybe I spotted the problem. The view is not executing because you have defined three urls with the exact regex in your project urls.py:
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^$', views.register, name='register'),
url(r'^$', views.projectz_save, name='project_save'),
)
Django match it's urls by iterating over the patterns in the way they appeared so in that file all urls will match index. That's probably the reason why the page appears to be refreshing. Try to modify this a little:
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^register$', views.register, name='register'),
url(r'^save$', views.projectz_save, name='project_save'),
)
This way you can execute the projectz_save method in the views.py if the action of the form matches the url regex.
Hope this helps!

Categories