我试图在我的索引页面上显示来自两个不同应用程序的内容,并具有两种不同的视图。
我为我的项目创建了一个 views.py 来处理索引页面,我正在尝试将我的两个应用程序中的模型实例连接到它。我没有收到任何错误,但是除了我的 base.html 中的内容之外,我无法显示任何内容。
这是代码和图片,以显示正在发生的事情与我正在尝试做的事情。
项目视图.py:
from django.shortcuts import render
from colorsets.models import ColorSet
from adminpanel.models import Widget
from django.utils import timezone
from django.contrib.auth import authenticate,login,logout
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse,reverse_lazy
def home(request, template='index.html'):
context = {
'widget': ColorSet.objects.all(),
'color_set': Widget.objects.all(),
}
return render(request, template, context)
项目 urls.py:
"""colors URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include
from accounts import views
from colorsets import views
from colors import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$',views.home,name='index'),
url(r'^accounts/',include('accounts.urls',namespace='accounts')),
url(r'^colorsets/',include('colorsets.urls',namespace='colorsets')),
url(r'^adminpanel/',include('adminpanel.urls',namespace='adminpanel')),
]
colorsets 应用程序 urls.py:
from django.conf.urls import url
from colorsets import views
app_name = 'colorsets'
urlpatterns = [
url(r'^new/$',views.CreateColorSetView.as_view(),name='new_color'),
url(r'delete/(?P<pk>\d+)/$',views.DeletePost.as_view(),name='delete'),
]
管理面板 urls.py:
from django.conf.urls import url
from adminpanel import views
app_name = 'adminpanel'
urlpatterns = [
url(r'^widgets/',views.SettingsListView.as_view(),name='widgets'),
url(r'^new/$',views.CreateWidgetView.as_view(),name='create-widget'),
url(r'^delete/$',views.DeleteWidget.as_view(),name='delete-widgets'),
]
管理面板应用程序 views.py:
from django.shortcuts import render
from adminpanel.forms import WidgetForm
from adminpanel.models import Widget
from django.utils import timezone
from django.contrib.auth import authenticate,login,logout
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse,reverse_lazy
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from braces.views import SelectRelatedMixin
from django.views.generic import (TemplateView,ListView,
DetailView,CreateView,
UpdateView,DeleteView)
# Create your views here.
class CreateWidgetView(LoginRequiredMixin,CreateView):
login_url = '/login/'
redirect_field_name = 'index.html'
form_class = WidgetForm
model = Widget
def form_valid(self,form):
self.object = form.save(commit=False)
self.object.save()
return super().form_valid(form)
def get_success_url(self):
return reverse('adminpanel:widgets')
class SettingsListView(ListView):
model = Widget
ordering = ['widget_order']
class DeleteWidget(LoginRequiredMixin,SelectRelatedMixin,DeleteView):
model = Widget
select_related = ('Widget',)
success_url = reverse_lazy('settings')
def get_queryset(self):
queryset = super().get_query()
return queryset.filter(user_id=self.request.user.id)
def delete(self):
return super().delete(*args,**kwargs)
颜色集应用程序 views.py:
from django.shortcuts import render
from colorsets.forms import ColorForm
from colorsets import models
from colorsets.models import ColorSet
from django.utils import timezone
from django.contrib.auth import authenticate,login,logout
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse,reverse_lazy
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from braces.views import SelectRelatedMixin
from django.views.generic import (TemplateView,ListView,
DetailView,CreateView,
UpdateView,DeleteView)
# Create your views here.
#def index(request):
# return render(request,'index.html')
class PostListView(ListView):
model = ColorSet
def get_queryset(self):
return ColorSet.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')
class CreateColorSetView(LoginRequiredMixin,CreateView):
login_url = '/login/'
redirect_field_name = 'index.html'
form_class = ColorForm
model = ColorSet
def form_valid(self,form):
self.object = form.save(commit=False)
self.object.user = self.request.user
self.object.save()
return super().form_valid(form)
class DeletePost(LoginRequiredMixin,SelectRelatedMixin,DeleteView):
model = models.ColorSet
select_related = ('user',)
success_url = reverse_lazy('index')
def get_queryset(self):
queryset = super().get_queryset()
return queryset.filter(user_id=self.request.user.id)
def delete(self,*args,**kwargs):
return super().delete(*args,**kwargs)
索引.html:
{% extends "base.html" %}
{% block content %}
<div class="container">
{% include "adminpanel/widget_list" %}
<div class="sidebar">
</div>
<div class="content">
{% for colorset in colorset_list %}
<div class="colorset-info">
<h3 class="set-name">{{ colorset.name }}</h3>
<p class="author accent-text">Author: {{ colorset.user }}</p>
{% if user.is_authenticated and colorset.user == user %}
<a class="auth-user-options" href="{% url 'colorsets:delete' pk=colorset.pk %}">Delete</a>
{% endif %}
</div>
<table class="colorset">
<tr>
<td class="color" style="background-color:#{{ colorset.color_one }}">
</td>
<td class="color" style="background-color:#{{ colorset.color_two }}">
</td>
<td class="color" style="background-color:#{{ colorset.color_three }}">
</td>
<td class="color" style="background-color:#{{ colorset.color_four }}">
</td>
<td class="color" style="background-color:#{{ colorset.color_five }}">
</td>
</tr>
<tr>
<td>
<p>#{{ colorset.color_one }}</p>
</td>
<td>
<p>#{{ colorset.color_two }}</p>
</td>
<td>
<p>#{{ colorset.color_three }}</p>
</td>
<td>
<p>#{{ colorset.color_four }}</p>
</td>
<td>
<p>#{{ colorset.color_five }}</p>
</td>
</tr>
</table>
{% endfor %}
</div>
</div>
{% endblock %}
widget_list.html:
{% extends "base.html" %}
{% block content %}
<div class="container">
<div class="content">
<div class="widgets-list">
{% for widget in widget_list %}
<h3>{{ widget.name }}</h3>
<h3>{{ widget.widget_order }}</h3>
<div>
<p>{{ widget.body }}</p>
</div>
{% endfor %}
</div>
<a class="#" href="{% url 'adminpanel:create-widget' %}">New Widget</a>
</div>
</div>
{% endblock %}
First of all the views.py assign the wrong context names, just swap them.
Second there is a problem in how you built your templates:
The index.html include the widget_list
(without ".html"!) this code define the content of block content
. And is already called in a block content.
In my opinion you should create a widget_list_inner.html
that contains only the code you want to reuse in other parts of the application.
The new widget_list.html
{% extends "base.html" %}
{% block content %}
<div class="container">
<div class="content">
{% include "adminpanel/widget_list_inner.html" %}
<a class="#" href="{% url 'adminpanel:create-widget' %}">New Widget</a>
</div>
</div>
{% endblock %}
widget_list_inner.html
<div class="widgets-list">
{% for widget in widget_list %}
<h3>{{ widget.name }}</h3>
<h3>{{ widget.widget_order }}</h3>
<div>
<p>{{ widget.body }}</p>
</div>
{% endfor %}
</div>
Now you can include in your index.html the widget_list_inner
in any place of the index.html
Apply the same thing for the color_set.html. Yes, move the code out from the index.html
. Take a look at the documentation of the include: https://docs.djangoproject.com/en/1.11/ref/templates/builtins/#include
如您所见,您可以将上下文值传递给您的包含,这有助于*_inner.html
在您的应用程序中的任何地方重用模板。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句