I am a django beginner even programming beginner this issue I have found one day still can not work out
views.py
from django.shortcuts import render, redirect
from django.template import loader
from .models import Topic
from django.contrib.auth.models import User
from .forms import TopicForm
# Create your views here.
def home(request):
q = request.Get.get('q')
topics = Topic.objects.filter(name__icontains=q)
context = {'topics':topics}
template = loader.get_template('home.html')
return render(request, 'home.html', context)
models.py
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Topic(models.Model):
name = models.CharField(max_length=200)
description = models.TextField()
home.html
<body>
{% include 'navbar.html' %}
<div>
<div>
<h3>Topic</h3>
{% for topic in topics %}
{{topic.name}}
{% endfor %}
</div>
</div>
</body>
I tried if statment still not work
Related
trying to create an edit profile for users and i keep getting this error what should i add or change ? is my models right for UserEditView
this is my views.py (all of it edited)
maybe the vendor its not compatitable with User edit view
anything elses needs to be added or should i just change something
all imports are for vendor and UserEditView
from tkinter import Entry
from django.contrib.auth.models import User
from xml.dom.minidom import Entity
from django.contrib.auth import login
from django.contrib.auth.decorators import login_required
from django.urls import reverse_lazy
from django.views import generic
from django.contrib.auth.forms import UserCreationForm , UserChangeForm
from django.utils.text import slugify
from django.shortcuts import render, redirect
from .models import Profile, Vendor
from products.models import Product
from .forms import ProductForm
# Create your views here.
def become_vendor(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
user = form.save()
login(request, user)
vendor = Vendor.objects.create(name=user.username, created_by=user)
return redirect('home')
else:
form = UserCreationForm()
return render(request, 'vendor/become_vendor.html', {'form': form})
#login_required
def vendor_admin(request):
context = {
'user':request.user
}
vendor = request.user.vendor
products = vendor.products.all()
return render(request,'vendor/vendor_admin.html',{'vendor': vendor , 'products': products ,'context':context})
#login_required
def add_house(request):
if request.method == 'POST':
form = ProductForm (request.POST, request.FILES)
if form.is_valid():
product = form.save(commit=False)
product.vendor = request.user.vendor
product.slug = slugify(product.عنوان)
product.save()
return redirect('vendor_admin')
else:
form = ProductForm()
return render(request,'vendor/add_house.html',{'form': form})
class UserEditView(generic.UpdateView):
models = User
form_class = UserChangeForm
template_name = 'vendor/edit_profile.html'
seccess_url = reverse_lazy('vendor_admin')
def get_object(self):
return self.request.user
urls.py
from django.urls import path
from .import views
from .views import UserEditView
from django.contrib import admin
from django.contrib.auth import views as auth_views
urlpattern =[
path('signup/', views.become_vendor, name='become_vendor'),
path('profile/', views.vendor_admin, name='vendor_admin'),
path("logout/", auth_views.LogoutView.as_view(), name="logout"),
path('login/', auth_views.LoginView.as_view(template_name='vendor/login.html'), name='login'),
path('edit_profile/', UserEditView.as_view(template_name='vendor/edit_profile.html'), name='edit_profile'),
]
edit_profile.html
(where the error pops up)
{% extends "base.html"%}
{% load static %}
{% block content %}
<title>title</title>
<div class="section pt-9 pb-9">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="section-title">
<div class="wrap-title">
<h3 class="title">
<span class="first-word"></span>
</h3>
<br>
<form method="post" >
{% csrf_token %}
<table>
{{ form.as_p }}
</table>
<button class='button'>Update</button>
</form>
</div>
<hr>
{% endblock content %}
I think that you didn't declare your model correctly:
class UserEditView(generic.UpdateView):
# models = UserChangeForm #That has no sense.
model = User #The name of your model (Probably the default one: User).
form_class = UserChangeForm
template_name = 'vendor/edit_profile.html'
success_url = reverse_lazy('vendor_admin')
def get_object(self):
return self.request.user
Other thing. You have declared your template name twice. According to your views.py you can delete the template_name on your urls.py:
path('edit_profile/', UserEditView.as_view(), name='edit_profile'),
I have an existing site, and I'm trying to render a form to it
I tried looping trough the form and rendering it field by field, but no luck. I think i might screwed up something in my views, im a beginner to django
HTML:
{%block wform%}
<form method="POST" class="form">
{% csrf_token %}
{{ form.as_p }}
<div class="form__group">
{%if user.is_authenticated%}
<input type="submit" class="submit" value="Submit Your Data">
{%else%}
Submit Your Data
{%endif%}
</div>
</form>
{%endblock wform%}
Forms.py
from django.forms import ModelForm
from users.models import Profile
class WeightForm(ModelForm):
class Meta:
model = Profile
fields = ['weight','height','goal']
Views.py
from django.shortcuts import render
from django.contrib import messages
from users import models
from users.models import Profile
from .forms import WeightForm
# Create your views here.
def home(request):
return render(request, 'Landing/index.html')
def formsave(request):
form = WeightForm()
return render(request, 'Landing/index.html', {'form': form})
from django.shortcuts import render
from django.contrib import messages
from users import models
from users.models import Profile
from .forms import WeightForm
# Create your views here.
def home(request):
form = WeightForm()
return render(request, 'Landing/index.html', {'form': form})
update is working but template form is not showing existing data. django updateview not showing existing data to form. when update page is showing only existing file is showing but object data is not showing. updateview cannot send existing data to form
view.py
from django.shortcuts import render, redirect
from django.core.files.storage import FileSystemStorage
from .forms import BookForm
from .models import Book
from django.views.generic import TemplateView, ListView, CreateView, UpdateView
from django.urls import reverse_lazy
class BookUpdate(UpdateView):
model = Book
form_class = BookForm
success_url = reverse_lazy('class_book_list')
template_name = 'updatebook.html'
model.py
from django.db import models
from django.urls import reverse
# Create your models here.
class Book(models.Model):
title = models.CharField(max_length=100)
author = models.CharField(max_length=100)
pdf = models.FileField(upload_to='books/pdfs/')
cover = models.ImageField(upload_to='books/covers/', null=True, blank=True)
def __str__(self):
return self.title
forms.py
from django import forms
from .models import Book
class BookForm(forms.ModelForm):
class Meta:
model = Book
fields = ('title', 'author', 'pdf', 'cover')
urls.py
from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
from . import views
urlpatterns = [
path('books/', views.book_list, name='book_list'),
path('books/upload/', views.upload_book, name='upload_book'),
path('books/<int:pk>/',views.delete_book, name='delete_book'),
path('class/books/',views.BookListView.as_view(), name='class_book_list'),
path('class/books/upload/',views.UploadBookView.as_view(), name='class_upload_book'),
path('class/books/update/<int:pk>/',views.BookUpdate.as_view(), name='class_update_book'),'''
updatebook.html
{% load crispy_forms_tags %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2> Upload Book to Database</h2>
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{form | crispy}}
<button type="submit" >Update</button>
</form>
</body>
</html>
html form cannot display existing data from database
please help
Make your method to GET i.e. (method="GET") in the form from where you are requesting the UpdateView page.
I have a problem with creating a view with data from the database. I created a view that should download data from videos (var films) and display them, unstable
views.py
from .models import Films
def index(request):
filmy = Films.objects
return render(request, 'films/index.html',{'filmy':filmy})
index.html
<h1>Films</h1>
{% for film in films.all %}
{{filmy.summary}}
<br>
{% endfor %}
models.py
class Films(models.Model):
image = models.ImageField(upload_to='images/')
summary = models.CharField(max_length=200)
def __str__(self):
return self.summary
I only have a blank page.
Your views.py
from django.shortcuts import render
from django.http import HttpResponse
from .models import Films
# Create your views here.
def index(request):
films = Films.objects.all()
return render(request, 'films/index.html',{'films':films})
In index.html
{% for film in films %}
<p> {{film.summary}} </p>
{% endfor %}
I hope that helps.
I'm new in Django, I researched how to create drop-down. I tried everything, but still, the dropdown is not showing in HTML view.
My sample script is this.
This is the model.py
from django.db import models
class samplemodel(models.Model):
list = (
('sample1','SAMPLE1'),
('sample2','SAMPLE2'),
)
list_choices = models.CharField(max_length=6, choices=list)
The forms.py
from .models import samplemodel
from django.forms import ModelForm
class sampleForm(ModelForm):
class Meta:
model = samplemodel
fields = ['list_choices']
This is the views.py
from django.shortcuts import render
from .models import samplemodel
from .forms import sampleForm
from django.views.generic import CreateView
class sampleView(CreateView):
model = samplemodel
form_class = sampleForm
template_name = 'home/sample.html'
This is the home.html
{% extends 'base.html' %}
{% block head %}
<title>Home</title>
{% endblock %}
{% block body %}
<div class="container">
<form class="post">
{% csrf_token %}
{{ form.as_p }}
</form>
</div>
{% endblock %}
I tried everything that I saw on the internet. But I can't still show the drop-down. I don't know if the imports are the issue or not.
you model should be like this
from django.db import models
class SampleModel(models.Model):
list = (
('sample1','SAMPLE1'),
('sample2','SAMPLE2'),
)
list_choices = models.CharField(max_length=6, choices=list)
in your urlconf
from django.views.generic import CreateView
from app.models import Samplemodel
from django.forms import ModelForm
url(r'^sample_form$',CreateView.as_view(model=SampleModel, template_name='home/sample.html', success_url='index', name='sample_form'))
thats all you don't need forms.py . django will take care of everything