Django sitemap静态页面

时间:2016-07-02 16:39:44

标签: python django sitemap

我有一个包含内容动态网页的网站,会定期添加和删除。除此之外,该网站还有静态页面,如/,/ about,/ how-it-works等。 我已将sitemaps.py文件配置为加载站点地图中的所有动态内容页面。

sitemap.xml的

...
<url>
<loc>
https://www.mywebsite.com/record?type=poem&id=165
</loc>
<changefreq>weekly</changefreq>
<priority>0.5</priority>
</url>
...

sitemaps.py

from django.contrib.sitemaps import Sitemap

from website.models import Content

class MySitemap(Sitemap):
    changefreq = "weekly"
    priority = 0.5

    def items(self):
        return Content.objects.all()

models.py

class Content(models.Model):
    content_type = models.CharField(max_length=255)
    ...
    def get_absolute_url(self):
        return '/record?type=' + self.content_type + '&id=' + str(self.id)

如何在我的站点地图中添加这些静态页面(/,/ about etc)? 谢谢!

1 个答案:

答案 0 :(得分:2)

经过一番搜索,我找到了Django Sitemaps and "normal" views。按照Matt Austin的回答,我能够实现我想要的目标。我将离开我所做的事情,以备将来参考。

<强> sitemaps.py

package com.example.harsimar.tabswithswipableviews;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;

public class MainActivity extends AppCompatActivity {
    private String[] tabNames = {"First", "Second", "Third"};
    Toolbar toolbar;
    TabLayout tabLayout ;
    ViewPager viewPager;
    TagPagerAdapter tagPagerAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar=(Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        tabLayout=(TabLayout)findViewById(R.id.tablayout);
        viewPager=(ViewPager)findViewById(R.id.viewPager);
        tagPagerAdapter=new TagPagerAdapter(getSupportFragmentManager());
        tagPagerAdapter.addFragments(new FirstTab(),"fragment tab 1");
        tagPagerAdapter.addFragments(new SecondTab(),"fragment tab 2");
        tagPagerAdapter.addFragments(new ThirdTab(),"fragment tab 3");
        viewPager.setAdapter(tagPagerAdapter);
        tabLayout.setupWithViewPager(viewPager);

    }
}

And this is the TagPagerAdapter class file



package com.example.harsimar.tabswithswipableviews;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.ArrayList;

/**
 * Created by Harsimar on 02-07-2016.
 */
public class TagPagerAdapter extends FragmentPagerAdapter {
    ArrayList<Fragment> fragments=new ArrayList<>();
    ArrayList<String> tabTitles =new ArrayList<>();

    public void addFragments(Fragment fragments,String titles){
        this.fragments.add(fragments);
        this.tabTitles.add(titles);

    }

    public TagPagerAdapter(FragmentManager fm){
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
       return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return tabTitles.get(position);
    }
}


The main layout xml looks like this



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context="com.example.harsimar.tabswithswipableviews.MainActivity">
    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        >
        <include
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            layout="@layout/toolbar_layout"
            />
        <android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/tablayout"
            app:tabMode="fixed"
            app:tabGravity="fill">
        </android.support.design.widget.TabLayout>
        <android.support.v4.view.ViewPager
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/viewPager">
        </android.support.v4.view.ViewPager>
    </android.support.design.widget.AppBarLayout>

</RelativeLayout>

this is just a basic app to test and learn tab views
but app just crashes before starting 
log cat shows


07-02 23:08:11.604 22298-22298/com.example.harsimar.tabswithswipableviews I/art: Not late-enabling -Xcheck:jni (already on)
07-02 23:08:12.103 22298-22298/com.example.harsimar.tabswithswipableviews W/System: ClassLoader referenced unknown path: /data/app/com.example.harsimar.tabswithswipableviews-2/lib/x86
07-02 23:08:14.171 22298-22304/com.example.harsimar.tabswithswipableviews W/art: Suspending all threads took: 7.484ms
07-02 23:08:15.678 22298-22304/com.example.harsimar.tabswithswipableviews W/art: Suspending all threads took: 8.872ms
07-02 23:08:23.459 22298-22298/com.example.harsimar.tabswithswipableviews W/System: ClassLoader referenced unknown path: /data/app/com.example.harsimar.tabswithswipableviews-2/lib/x86
07-02 23:08:23.791 22298-22304/com.example.harsimar.tabswithswipableviews W/art: Suspending all threads took: 6.128ms
07-02 23:08:24.038 22298-22298/com.example.harsimar.tabswithswipableviews W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
07-02 23:08:24.460 22298-22298/com.example.harsimar.tabswithswipableviews D/AndroidRuntime: Shutting down VM
07-02 23:08:24.463 22298-22298/com.example.harsimar.tabswithswipableviews E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                            Process: com.example.harsimar.tabswithswipableviews, PID: 22298
                                                                                            java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.harsimar.tabswithswipableviews/com.example.harsimar.tabswithswipableviews.MainActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
                                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                                at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                                at android.os.Looper.loop(Looper.java:148)
                                                                                                at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                                             Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
                                                                                                at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:197)
                                                                                                at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:126)
                                                                                                at com.example.harsimar.tabswithswipableviews.MainActivity.onCreate(MainActivity.java:23)
                                                                                                at android.app.Activity.performCreate(Activity.java:6237)
                                                                                                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                                at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                                at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                                at android.os.Looper.loop(Looper.java:148) 
                                                                                                at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                                at java.lang.reflect.Method.invoke(Native Method) 
                                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
07-02 23:09:02.204 22298-22298/com.example.harsimar.tabswithswipableviews I/Process: Sending signal. PID: 22298 SIG: 9

<强> urls.py

from django.contrib.sitemaps import Sitemap
from django.core.urlresolvers import reverse

from website.models import Content

class StaticSitemap(Sitemap):
    """Reverse 'static' views for XML sitemap."""
    changefreq = "daily"
    priority = 0.5

    def items(self):
        # Return list of url names for views to include in sitemap
        return ['landing', 'about', 'how-it-works', 'choose']

    def location(self, item):
        return reverse(item)

class DynamicSitemap(Sitemap):
    changefreq = "daily"
    priority = 0.5

    def items(self):
        return Content.objects.all()