Reputation: 209
I have a idea to create something like a admin panel. In that panel the user (or admin as well) for example in my case will can add a field named "website". And in this moment I have a problem. I've create in myapp model Website:
from django.db import models
from django.contrib.auth.models import User
class Website(models.Model):
user = models.ForeignKey(User,related_name="website_user", on_delete=models.CASCADE)
website = models.CharField(help_text="Podaj stronę wraz z http lub https",max_length=250,verbose_name='Strona www', unique=True)
class Meta:
verbose_name = 'Strona www'
verbose_name_plural = 'Strony www'
def __str__(self):
return self.website
after that I've create a form to display for the user when is loggin:
from django import forms
from django.contrib.auth.models import User
from .models import Website
class WebsiteForm(forms.ModelForm):
class Meta:
model = Website
fields = ('website',)
Next step was a creating view :
from django.shortcuts import render
from django.contrib.auth import authenticate, login
from django.contrib.auth.models import User
from .forms import LoginForm, UserRegistrationForm, WebsiteForm
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from .models import Website
...
@login_required
def add_website(request):
if request.method == 'POST':
website_form = WebsiteForm(data=request.POST)
if website_form.is_valid():
new_website = website_form.save(commit=False)
new_website.post = add_website
new_website.save()
else:
website_form = WebsiteForm()
return render(request, 'konto/add_website.html', {'add_website': add_website, 'website_form': website_form})
and on the end I've create a html file responsible for displaying form and action
{% if request.user.is_authenticated %}
{% if new_website %}
<h2>Twója strona oczekuje na zaakceptowanie</h2>
{% else %}
<h4>Dodaj stronę do monitorowania</h4>
<form action="." method="POST">
{{website_form.as_p}}
{% csrf_token %}
<p><input type="submit" value="Dodaj stronę"></p>
</form>
{% endif %}
<a href="{% url 'dashboard' %}">Powrót</a>
{% endif %}
My problem is that, when I'am login as for example admin, I trying to add new record using this form. But the nothing is happen. I fill the form, and after click button the script redirecting me to the dashboard. In dashboard I have a blank list of added websites. part of code in dashboard.html
...
{% if request.user.is_authenticated %}
Twoja www:
{% for website in websites %}
{{website.website}}
{% endfor %}
{% endif %}
...
and the view for displaying list of websites:
@login_required
def website(request):
websites = Website.objects.all().order_by('-User')
context = {'websites:', websites}
return render(request, 'konto/dashboard.html', context=context)
I want to make a list of pages, so that the user who added the page, only saw his records and not all that exist in the database. EDIT: And sometimes when I change form link I have a error
IntegrityError at /konto/add-website/
NOT NULL constraint failed: konto_website.user_id
EDIT:
Now I have something like this:
@login_required
def new_website(request):
if request.method == "POST":
new_website = WebsiteForm(request.POST)
if new_website.is_valid():
new_website=new_website.save(commit=False)
new_website.user = request.user
new_website.save()
messages.success(request, 'Pomyślnie dodano stronę')
return render(request, 'konto/add_website_ok.html')
else:
new_website = WebsiteForm()
return render(request, 'konto/add_website.html', {'new_website':new_website})
Everything is working ok, if I'am login as admin, I can add new website as a admin. If I login for example as a user, I can add new website as a user. In Django Admin I can see every record whos was added by users. But I've create also something like dashboard in my own control panel. And in that control panel I want to display the list of websites added by users. For example, if I'am login as 'user' i can see only my webpages, not all from the list. I want to display it in dashboard.html as:
{% if request.user.is_authenticated %}
<b>Monitoring www:</b>
{% for website in website_list %}
{{website.website}}
{% endfor %}
{% endif %}
But now I see only a label 'Monitoring www', nothing else.. I think You understand me. :)
2nd EDIT
view @login_required
def website(request):
website_list = Website.objects.filter(user=request.user).order_by('-User')
context = {'website_list:', website_list}
return render(request, 'konto/dashboard.html', context=context)
html
{% for website in website_list %}
{{website.website}}
{% endfor %}
{% endif %}
Upvotes: 0
Views: 127
Reputation: 5793
I'm not sure what you're doing here
if website_form.is_valid():
new_website = website_form.save(commit=False)
new_website.post = add_website
new_website.save()
But I think it should work if it is simplified to:
if website_form.is_valid():
new_website = website_form.save()
@login_required
def add_website(request):
variables = {}
if request.method == 'POST':
website_form = WebsiteForm(data=request.POST)
if website_form.is_valid():
new_website = website_form.save(commit=False)
new_website.post = add_website
new_website.save()
else:
website_form = WebsiteForm()
variables['add_website'] = add_website
variables['website_form'] = website_form
if new_website: variables['new_website'] = True
return render(request, 'konto/add_website.html', variables)
Upvotes: 1