My project is to build a supermarket website.customers can input the quantity of the products so that I can calculate price.I use the first product 'apple' to test for whether I can get the typing integer by the customers and show on the price.html.
My expected result is '1'I have tried many times but
it still show none.Can anyone help me?
my views code is like this:
def price(request):
a = request.GET.get('apple')
return render(request, 'price.html', {'a': a})
meat-series html file is like:
{% block content %}
<body>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.2.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-iYQeCzEYFbKjA/T2uDLTpkwGzCiq6soy8tYaI1GyVh/UjpbCx/TYkiZhlZB6+fzT" crossorigin="anonymous">
{% for ser in meat_ser %}
<div style="position:sticky ;" class= "row" >
<div class="col-sm-6">
<div class="card mb-3 " style="width: 18rem;">
<img style="height: 250px;margin: 0%;" id='photo'src="{{ser.photo}}" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">{{ser.Name}}</h5>
<p class="card-text"></p>
<form action="price/" >
<label form='price/'>Qualitiy:</label>
<input type="number" name={{ser.Name}}>
<input type="submit" value="Buy">
</form>
The urls file is like this:
from django.urls import path, re_path
from . import views
app_name = 'web'
urlpatterns = [
path('', views.index, name='index'),
re_path(r'^login/', views.login_request, name='login'),
re_path(r'^register/', views.register_request, name='register'),
re_path(r'^meat_series/', views.meat_series, name='meat_series'),
path('logout/', views.logout_request, name='logout'),
path('price/', views.price, name='price')
]
The url showed when submitted is: http://127.0.0.1:8000/web/meat_series/price/?apple=1
Related
I am building an app where users can access their archives through a simple slug, as follows:
lekha.cc/<archive_slug>
This is exactly as instagram does it. However, whenever I go to any other page, such as
lekha.cc/dashboard
The code for the archive view runs, saying that it has not found an archive with that slug. This is an issue for 2 reasons: we dont want any excess code to run, and if a user chooses to name their archive 'dashboard', the entire website would potentially break down since no one would be able to access their dashboard.
My urls.py folder is as follows:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('main.urls'), name='index'),
path('onboarding/', account_views.onboarding, name='onboarding'),
path('register/', account_views.register, name='register'),
path('login/', auth_view.LoginView.as_view(authentication_form=LoginForm, template_name='accounts/login.html'), name="login"),
path('logout/', account_views.logout_view, name='logout'),
path('dashboard/', archival_views.dashboard, name='dashboard'),
path('account_settings/', account_views.account_settings, name='account_settings'),
path('<str:slug>/', main_views.archive, name='archive'),
path('item/<str:slug>/', main_views.work, name='work'),
]
Does anyone have any solutions to this issue?
EDIT:
Here is the code for the dashboard view
def dashboard(request):
user = get_current_user()
archive = Archive.objects.get(creator=user)
filesystem = Folder.objects.get(archive=archive)
if request.method == "POST":
if 'addCategory' in request.POST:
category_name = request.POST['folderName']
filesystem = Folder.objects.get(pk=filesystem.pk)
filesystem.add_child(name=category_name)
return render(request, "archival/dashboard.html", {'filesystem': filesystem, "archve": archive, "fileSystemParse": filesystem.get_annotated_list()})
And the archive view
def archive(request, slug):
# retrieve archive with the corresponding slug requested (lekha.cc/dhruva will return the archive with slug='dhruva')
archive = Archive.objects.get(archive_slug=slug)
filesystem = Folder.objects.get(archive=archive)
return render(request, 'archive.html', {'archive': archive, 'filesystem': filesystem})
And the dashboard template:
<html lang="en">
<head>
<style>
</style>
</head>
</html>
{% extends 'navbar.html' %}
{% block content %}
{% load static %}
<div style="height: 200px; width: 100%;"></div>
<p>
archive: {{ archive.archive_slug }}, filesystem: {{ filesystem.name }}
</p>
<div id="folder_view">
{% include 'partials/folder_view.html' %}
</div>
<input type="button" value="addFolder">
<input type="button" value="addFile">
<form action="/dashboard/" method="post">
{% csrf_token %}
<input type="text" name="folderName">
<input type="submit" value="Add Category" name="addCategory">
</form>
<!-- Popups -->
<div id="new_folder_popup" class="dashboard-popup">
<div class="dashboard-popup-content">
<span class="close">×</span>
<!-- <form action="/dashboard/" method="post">
{% csrf_token %}
<input type="text" name="folderName">
<input type="submit" value="Add Category" name="addCategory">
</form> -->
</div>
</div>
The issue was caused by the browser requesting /favicon.ico/ which django was passing through my view. The url.py file should in theory look through all the URLs in order until it finds the right view, so by default django already checks URLs only once.
The real issue is described further here
So in general when a user logs in, my app redirects him to dashboard-overview:
# settings.py
# Login and Logout target routes
LOGIN_URL = 'login'
LOGIN_REDIRECT_URL = 'dashboard-overview'
LOGOUT_REDIRECT_URL = '/'
However for a specific case I want the user to be redirected to 'calculator-page' using a helper function which doesn't work as the user is again redirected to the value of LOGIN_REDIRECT_URL.
# views.py
def render_calculator(request, slug):
"""
Renders the calculator page
"""
# Check for the user being authenticated
if not request.user.is_authenticated:
return redirect_to_login('calculator-page')
# routes to http://127.0.0.1:8000/login/?next=calculator-page which looks fine,
# but then it fails after login to re-route as specified and sends to
# 'dashboard-overview' again
else:
club_obj = Offer.objects.get(club__slug=slug)
# Create Context
context = {
'club_obj': club_obj,
}
return render(request, 'core/calculator.html', context)
# urls.py
from django.urls import path, include
from django.contrib.auth import views
from applications.user import views as user_views
urlpatterns = [
# Login View
path('login/', views.LoginView.as_view(template_name='user/login.html'), name="login"),
path('logout/', views.LogoutView.as_view(), name="logout"),
# Sign Up
path('signup/', user_views.signup, name='signup'),
]
# template user/login.html
{% extends 'user/user_frame.html' %}
{% load static %}
<!-- Styles -->
{% block styles %}
<link href="{% static 'css/user_frame.css' %}" rel="stylesheet" type="text/css">
{% endblock styles %}
{% block form %}
<div class="logo-container"><img src="{% static 'images/logo_negative_text.svg' %}" class="logo-image"></div>
<div class="form-container">
<!-- form headline -->
<h1 class="form-headline">Login to your Farena Account</h1>
<!-- form -->
<form class="form" method="post" action=".">
{% csrf_token %}
<div class="error-wrapper">
<div class="errors">{{ form.non_field_errors }}</div>
</div>
<div class="email-wrapper field-wrapper">
<div class="tag name-tag">{{ form.username.label }}*</div>
<div class="input">{{ form.username }}</div>
</div>
<div class="password-wrapper field-wrapper">
<div class="tag">{{ form.password.label }}*</div>
<div class="input">{{ form.password }}</div>
<div class="forgot-password">Forgot Password?</div>
</div>
<!-- submit button -->
<button class="login-button" type="submit">Log In</button>
</form>
</div>
<div class="account-info">No account yet? Sign Up!</div>
{% endblock form %}
The issue is with your template for the login view - you're missing the next form field that tells Django where to redirect the user after they have logged in. See the documentation for sample template code for this view.
Specifically, you need to add this inside your form:
<input type="hidden" name="next" value="{{ next }}">
That's missing right now which means Django will always just default to LOGIN_REDIRECT_URL.
I want to create a tempfriends during creating Moneybook.
So i write a code in a create moneybook template but error occur :
NoReverseMatch at /moneybooks/create/
Reverse for 'create' not found. 'create' is not a valid view function or pattern name.
I write a proper appname, write a urls.py, use single quote...ete but don't know how to achieve it.
moneybook_form.html
<div class="input {% if field.errors %}has_error{% endif %}">
<div class="flex">
<div class="w-1/4">
{{form.companion.label}}
</div>
<div class="w-3/4 flex inline border-b my-2 py-3">
<div class="w-3/4">
{{form.companion}}
</div>
<div class= "w-1/4 flex justify-center ">
<i class="fas fa-plus-circle"></i> #this part error occured
</div>
</div>
</div>
{% if form.companion.errors %}
{% for error in form.companion.errors %}
><span class="text-red-700 font-medium text-sm">{{error}}</span>
{% endfor %}
{% endif %}
</div>
config/urls.py
urlpatterns = [
path("", include("cores.urls", namespace="cores")),
path('admin/', admin.site.urls),
path("users/", include("users.urls", namespace="users")),
path("tempfriends/", include("tempfriends.urls", namespace="tempfriends")),
path("moneybooks/", include("moneybooks.urls", namespace="moneybooks")),
path("moneylogs/", include("moneylogs.urls", namespace="moneylogs"))
]
moneybooks/urls.py
app_name = "moneybooks"
urlpatterns = [
path("create/", views.moneybook_create.as_view(), name="create"),
path("update/<int:pk>/",
views.moneybook_update.as_view(), name="update"),
path("<int:pk>/", views.moneybook_detail, name="detail")
]
tempfriends/urls.py
from django.urls import path
from . import views
app_name = "tempfriends"
urlpatterns = [
path("create/", views.tempfriend_create.as_view(), name="create"),
]
tempfriends/views.py
class tempfriend_create(FormView):
form_class = forms.CreateTempfriendForm
template_name = "tempfriends/create.html"
def get_current_user(request):
current_user = request.user
def form_valid(self, form):
tempfriend = form.save()
belongs_to = current_user
form.instance.belongs_to = belongs_to
tempfriend.save()
return redirect(reverse("cores:home"))
I just checked my urls.py and I am including my url files without the namespace parameter and it works.
I'm preparing a blog component of a site, it is created inside an app of a project. It seems that there is no problem except the images are not displayed.
while running, all things are OK, but just the image "alt" is displayed instead of the image. I have images without problem in other pages of this project.
There is a report on Terminal:
"Not Found: /article/static/images/growth_EWAl68g.png
[14/Apr/2019 17:44:38] "GET /article/static/images/growth_EWAl68g.png HTTP/1.1" 404 6164"
As you can see, Django uses a false address (static folder is located in root of projects) to access the images.
class BlogArticle(models.Model):
title = models.CharField(max_length=150)
headline = models.CharField(max_length=300)
body = models.TextField()
post_image = models.ImageField(upload_to ="static/images")
date = models.DateTimeField(auto_now_add=True)
author = models.CharField(max_length=100)
def __str__(self):
return self.title
class ArticleListView(ListView):
model = BlogArticle
template_name = 'article_list.html'
urlpatterns = [
path('', views.HomeView, name= 'home'),
path('article/', views.ArticleListView.as_view(), name='article_list'),
path('article/<int:pk>/', views.ArticleDetailView.as_view(), name='article_detail'),
.
.
.]
from django.contrib import admin
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('contactus', include('sendemail.urls')),
path('', include('myapp.urls')),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
{% block content %}
<div <div class="container mt-5 bg-light">
{% for article in object_list %}
<div class="form-row">
<div class="form-group col-sm-9 ">
<div class="post-entry">
<h5 > <a href = "{% url 'article_detail' article.pk %}" > {{ article.title }}</a></h5>
<div>
<p class="font-weight-bold"> {{article.headline }} </p>
</div>
<div>
<span class="text-muted">by {{article.author }} | {{ article.date|date:"M d, Y"}}</span>
</div>
</div>
</div>
<div class="form-group col-sm-3 ">
<div class="thumbnail">
<img src="{{article.post_image}}" width=100 height=100 class="rounded" alt="{{article.title}}">
</div>
</div>
</div>
{% endfor %}
</div>
{% endblock content %}
.
DEBUG = True
ALLOWED_HOSTS = []
.
.
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'),]
.
.
I can't understand this condition, It seems that Django should go to static/images/ but it has an extra word (article) at the beginning of the address.
This problem was simply solved by adding
urlpatterns +=static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
to project's urls.py and
MEDIA_URL = '/media/'
in settings.py.
Following is my urls.py file:
from django.urls import path, re_path, include
from . import views
app_name = 'blog'
urlpatterns = [
path('', views.post_list, name='list'),
path('<slug>/', views.post_detail, name='detail'),
]
My views.py file:
from django.shortcuts import render
from .models import Post
# Create your views here.
def post_list(request):
posts = Post.objects.all().order_by('date')
return render(request, 'blog/post_list.html', {'posts': posts})
def post_detail(request, slug):
post_det = Post.objects.get(slug=slug)
return render(request, 'blog/post_detail.html', {'singlepost': post_det})
And my post_detail.html page:
{% extends 'base.html' %}
{% block content %}
<div class="container-fluid">
<div class="row">
{% for post in posts %}
<div class="post col-md-10 shadow mx-auto">
<!-- post-thumbnail -->
<div class="post-thumbnail">
<img src="{{singlepost.thumb.url}}">
</div>
<!-- /post-thumbnail -->
<h2 class="post-title bg-dark text-light pad1 text-center">{{ singlepost.title }}</h2>
<p class="post-content">{{ singlepost.body }}</p>
<p class="post-info grey border pad1">{{ singlepost.date }}</p>
</div>
{% endfor %}
</div>
</div>
{% endblock content %}
Screenshot from the error page:
Django - Page Not Found Error
What appears to be the problem here? Keep in mind that I'm new to django, however, this is the first time I come across an url such as the one noted in the error report.
I have made few changes,
In urls.py,
path('detail/<slug:slug>/', views.#restofyoururl ),
You have not added your homepage html, so check if you have added the url correctly. In your homepage html
<a href={% url 'blog:detail' post.slug %}> {{post.title}}#add as per your model </a>
Note : You can add class based view for your detail page.